使用typescript2和electron时遇到的tsd问题

发布于 2022-09-03 19:13:28 字数 15296 浏览 9 评论 0

想用最新的typescript2.0和electron写一个桌面应用
利用typescript2的新特性,用npm的@types/*来管理tsd文件
但是执行tsc编译时总是出现Duplicate identifier的问题
请问该如何解决

文件结构如下

*
|-main.ts
|-package.json
|-tsconfig.json
\-app
  |-index.html

package.json

{
  "name": "fuck",
  "version": "0.1.0",
  "main": "./main.js",
  "scripts": {
    "prestart": "tsc",
    "start": "electron ."
  },
  "dependencies": {
    "@types/electron": "^1.3.22",
    "@types/node": "^6.0.40",
    "electron": "^1.4.0"
  },
  "devDependencies": {
    "typescript": "^2.0.0"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "typeRoots": [
            "./node_modules/@types/"
        ],
        "types": [
            "node",
            "electron"
        ]
    },
    "exclude": [
        "node_modules"
    ]
}

main.ts

import {app, BrowserWindow} from 'electron';

class MyWindow extends BrowserWindow {
    constructor() {
        super({
            width: 800,
            height: 600
        });
        this.loadURL(`file://${__dirname}/app/index.html`);
    }
}

app.on("ready", () => {
    new MyWindow()
});

tsc执行结果如下

node_modules/.1.3.22@@types/electron/index.d.ts(10,8): error TS2300: Duplicate identifier 'EventEmitter'.                                                                         
node_modules/.1.3.22@@types/electron/index.d.ts(28,7): error TS2300: Duplicate identifier 'Point'.                                                                                
node_modules/.1.3.22@@types/electron/index.d.ts(33,7): error TS2300: Duplicate identifier 'Size'.                                                                                 
node_modules/.1.3.22@@types/electron/index.d.ts(38,7): error TS2300: Duplicate identifier 'Rectangle'.                                                                            
node_modules/.1.3.22@@types/electron/index.d.ts(435,7): error TS2300: Duplicate identifier 'AppPathName'.                                                                         
node_modules/.1.3.22@@types/electron/index.d.ts(590,7): error TS2300: Duplicate identifier 'SetJumpListResult'.                                                                   
node_modules/.1.3.22@@types/electron/index.d.ts(743,8): error TS2300: Duplicate identifier 'BrowserWindow'.                                                                       
node_modules/.1.3.22@@types/electron/index.d.ts(1355,7): error TS2300: Duplicate identifier 'SwipeDirection'.                                                                     
node_modules/.1.3.22@@types/electron/index.d.ts(1357,7): error TS2300: Duplicate identifier 'ThumbarButtonFlags'.                                                                 
node_modules/.1.3.22@@types/electron/index.d.ts(1743,7): error TS2300: Duplicate identifier 'BrowserWindowType'.                                                                  
node_modules/.1.3.22@@types/electron/index.d.ts(1744,7): error TS2300: Duplicate identifier 'BrowserWindowTypeLinux'.                                                             
node_modules/.1.3.22@@types/electron/index.d.ts(1745,7): error TS2300: Duplicate identifier 'BrowserWindowTypeMac'.                                                               
node_modules/.1.3.22@@types/electron/index.d.ts(1746,7): error TS2300: Duplicate identifier 'BrowserWindowTypeWindows'.                                                           
node_modules/.1.3.22@@types/electron/index.d.ts(1828,7): error TS2300: Duplicate identifier 'ClipboardType'.                                                                      
node_modules/.1.3.22@@types/electron/index.d.ts(2321,7): error TS2300: Duplicate identifier 'IpcMainEventListener'.                                                               
node_modules/.1.3.22@@types/electron/index.d.ts(2369,7): error TS2300: Duplicate identifier 'IpcRendererEventListener'.                                                           
node_modules/.1.3.22@@types/electron/index.d.ts(2384,8): error TS2300: Duplicate identifier 'MenuItem'.                                                                           
node_modules/.1.3.22@@types/electron/index.d.ts(2419,7): error TS2300: Duplicate identifier 'MenuItemType'.                                                                       
node_modules/.1.3.22@@types/electron/index.d.ts(2420,7): error TS2300: Duplicate identifier 'MenuItemRole'.                                                                       
node_modules/.1.3.22@@types/electron/index.d.ts(2421,7): error TS2300: Duplicate identifier 'MenuItemRoleMac'.                                                                    
node_modules/.1.3.22@@types/electron/index.d.ts(2530,8): error TS2300: Duplicate identifier 'Menu'.                                                                               
node_modules/.1.3.22@@types/electron/index.d.ts(2585,8): error TS2300: Duplicate identifier 'NativeImage'.                                                                        
node_modules/.1.3.22@@types/electron/index.d.ts(2762,7): error TS2300: Duplicate identifier 'FileProtocolHandler'.                                                                
node_modules/.1.3.22@@types/electron/index.d.ts(2763,7): error TS2300: Duplicate identifier 'BufferProtocolHandler'.                                                              
node_modules/.1.3.22@@types/electron/index.d.ts(2764,7): error TS2300: Duplicate identifier 'StringProtocolHandler'.                                                              
node_modules/.1.3.22@@types/electron/index.d.ts(2765,7): error TS2300: Duplicate identifier 'HttpProtocolHandler'.                                                                
node_modules/.1.3.22@@types/electron/index.d.ts(2878,7): error TS2300: Duplicate identifier 'DisplayMetrics'.                                                                     
node_modules/.1.3.22@@types/electron/index.d.ts(2927,8): error TS2300: Duplicate identifier 'Session'.                                                                            
node_modules/.1.3.22@@types/electron/index.d.ts(3029,7): error TS2300: Duplicate identifier 'Permission'.                                                                         
node_modules/.1.3.22@@types/electron/index.d.ts(3326,8): error TS2300: Duplicate identifier 'BeforeRequestCallback'.                                                              
node_modules/.1.3.22@@types/electron/index.d.ts(3338,8): error TS2300: Duplicate identifier 'BeforeSendHeadersCallback'.                                                          
node_modules/.1.3.22@@types/electron/index.d.ts(3356,8): error TS2300: Duplicate identifier 'HeadersReceivedCallback'.                                                            
node_modules/.1.3.22@@types/electron/index.d.ts(4441,7): error TS2300: Duplicate identifier 'NewWindowDisposition'.                                                               
node_modules/.1.3.22@@types/electron/index.d.ts(4449,7): error TS2300: Duplicate identifier 'StopFindInPageAtion'.                                                                
node_modules/.1.3.22@@types/electron/index.d.ts(4451,7): error TS2300: Duplicate identifier 'CursorType'.                                                                         
node_modules/.1.3.22@@types/electron/index.d.ts(5265,8): error TS2300: Duplicate identifier 'Event'.                                                                              
node_modules/.1.3.22@@types/electron/index.d.ts(5435,7): error TS2300: Duplicate identifier 'GlobalEvent'.                                                                        
node_modules/.1.3.22@@types/electron/index.d.ts(5575,2): error TS2300: Duplicate identifier 'export='.  
node_modules/@types/electron/index.d.ts(10,8): error TS2300: Duplicate identifier 'EventEmitter'.                                                                                 
node_modules/@types/electron/index.d.ts(28,7): error TS2300: Duplicate identifier 'Point'.                                                                                        
node_modules/@types/electron/index.d.ts(33,7): error TS2300: Duplicate identifier 'Size'.                                                                                         
node_modules/@types/electron/index.d.ts(38,7): error TS2300: Duplicate identifier 'Rectangle'.                                                                                    
node_modules/@types/electron/index.d.ts(435,7): error TS2300: Duplicate identifier 'AppPathName'.                                                                                 
node_modules/@types/electron/index.d.ts(590,7): error TS2300: Duplicate identifier 'SetJumpListResult'.                                                                           
node_modules/@types/electron/index.d.ts(743,8): error TS2300: Duplicate identifier 'BrowserWindow'.                                                                               
node_modules/@types/electron/index.d.ts(1355,7): error TS2300: Duplicate identifier 'SwipeDirection'.                                                                             
node_modules/@types/electron/index.d.ts(1357,7): error TS2300: Duplicate identifier 'ThumbarButtonFlags'.                                                                         
node_modules/@types/electron/index.d.ts(1367,3): error TS2374: Duplicate string index signature.                                                                                  
node_modules/@types/electron/index.d.ts(1743,7): error TS2300: Duplicate identifier 'BrowserWindowType'.                                                                          
node_modules/@types/electron/index.d.ts(1744,7): error TS2300: Duplicate identifier 'BrowserWindowTypeLinux'.                                                                     
node_modules/@types/electron/index.d.ts(1745,7): error TS2300: Duplicate identifier 'BrowserWindowTypeMac'.                                                                       
node_modules/@types/electron/index.d.ts(1746,7): error TS2300: Duplicate identifier 'BrowserWindowTypeWindows'.                                                                   
node_modules/@types/electron/index.d.ts(1828,7): error TS2300: Duplicate identifier 'ClipboardType'.                                                                              
node_modules/@types/electron/index.d.ts(2321,7): error TS2300: Duplicate identifier 'IpcMainEventListener'.                                                                       
node_modules/@types/electron/index.d.ts(2369,7): error TS2300: Duplicate identifier 'IpcRendererEventListener'.                                                                   
node_modules/@types/electron/index.d.ts(2384,8): error TS2300: Duplicate identifier 'MenuItem'.                                                                                   
node_modules/@types/electron/index.d.ts(2419,7): error TS2300: Duplicate identifier 'MenuItemType'.                                                                               
node_modules/@types/electron/index.d.ts(2420,7): error TS2300: Duplicate identifier 'MenuItemRole'.                                                                               
node_modules/@types/electron/index.d.ts(2421,7): error TS2300: Duplicate identifier 'MenuItemRoleMac'.                                                                            
node_modules/@types/electron/index.d.ts(2530,8): error TS2300: Duplicate identifier 'Menu'.                                                                                       
node_modules/@types/electron/index.d.ts(2585,8): error TS2300: Duplicate identifier 'NativeImage'.                                                                                
node_modules/@types/electron/index.d.ts(2762,7): error TS2300: Duplicate identifier 'FileProtocolHandler'.                                                                        
node_modules/@types/electron/index.d.ts(2763,7): error TS2300: Duplicate identifier 'BufferProtocolHandler'.                                                                      
node_modules/@types/electron/index.d.ts(2764,7): error TS2300: Duplicate identifier 'StringProtocolHandler'.                                                                      
node_modules/@types/electron/index.d.ts(2765,7): error TS2300: Duplicate identifier 'HttpProtocolHandler'.                                                                        
node_modules/@types/electron/index.d.ts(2878,7): error TS2300: Duplicate identifier 'DisplayMetrics'.                                                                             
node_modules/@types/electron/index.d.ts(2927,8): error TS2300: Duplicate identifier 'Session'.                                                                                    
node_modules/@types/electron/index.d.ts(3029,7): error TS2300: Duplicate identifier 'Permission'.                                                                                 
node_modules/@types/electron/index.d.ts(3326,8): error TS2300: Duplicate identifier 'BeforeRequestCallback'.                                                                      
node_modules/@types/electron/index.d.ts(3338,8): error TS2300: Duplicate identifier 'BeforeSendHeadersCallback'.                                                                  
node_modules/@types/electron/index.d.ts(3356,8): error TS2300: Duplicate identifier 'HeadersReceivedCallback'.                                                                    
node_modules/@types/electron/index.d.ts(4438,3): error TS2374: Duplicate string index signature.                                                                                  
node_modules/@types/electron/index.d.ts(4441,7): error TS2300: Duplicate identifier 'NewWindowDisposition'.                                                                       
node_modules/@types/electron/index.d.ts(4449,7): error TS2300: Duplicate identifier 'StopFindInPageAtion'.                                                                        
node_modules/@types/electron/index.d.ts(4451,7): error TS2300: Duplicate identifier 'CursorType'.                                                                                 
node_modules/@types/electron/index.d.ts(5265,8): error TS2300: Duplicate identifier 'Event'.                                                                                      
node_modules/@types/electron/index.d.ts(5435,7): error TS2300: Duplicate identifier 'GlobalEvent'.                                                                                
node_modules/@types/electron/index.d.ts(5575,2): error TS2300: Duplicate identifier 'export='.  

看上去像是npm在node_modules下按版本下载的packages和其软连接之间发生了重复定义的问题

node和electron使用了同样的方式管理tsd文件,但这个问题只在electron上出现

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2022-09-10 19:13:28

我本来想试一下,结果下了很久都没把 electron 下载下来。不过还好 @types 是下载下来了的。

看了下 electron 和 nodejs 在 @types 中的定义,确实有很多重复的类,但是这些类都定义在不同的命名空间中,所以理论上来说不清楚出现重复定义。

另外,我试了 tsc 编译你的代码(只不过我的文件名是 index.ts),毫无压力啊。不知道是不是 electron 的问题,不过看消息的样子又像是编译的时候出的问题

clipboard.png

椵侞 2022-09-10 19:13:28

electron 包含了 typescript 声明

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文