electron: electron.d.ts does not work with @types/node v13.1.0
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for an issue that matches the one I want to file, without success.
Issue Details
- Electron Version:
- 7.1.7
- Operating System:
- macOS 10.14
- Last Known Working Electron version:
- N/A
Expected Behavior
Can compile successfully the TypeScript code
Actual Behavior
Compilation error
main/window.ts:14:5 - error TS2339: Property 'removeAllListeners' does not exist on type 'BrowserWindow'.
14 win.removeAllListeners();
~~~~~~~~~~~~~~~~~~
node_modules/electron/electron.d.ts:1655:31 - error TS2689: Cannot extend an interface 'NodeJS.EventEmitter'. Did you mean 'implements'?
1655 class BrowserWindow extends NodeJS.EventEmitter {
~~~~~~
EDIT: Added entire compile error messages thanks to https://github.com/electron/electron/issues/21612#issuecomment-568691944
To Reproduce
Try to compile following code with the latest TypeScript compiler and latest @types/node package.
import { BrowserWindow } from 'electron';
const win = new BrowserWindow();
win.removeAllListeners();
Package versions:
typescript: 3.7.4@types/node: 13.1.0
Screenshots
Nothing
Additional Information
I have investigated this issue and found the cause.
@types/node v13 has breaking change affecting NodeJS.EventEmitter from v12. It was changed from class to interface. Now, EventEmitter can only be extended by interfaces. I confirmed electron.d.ts worked fine with @types/node v12. The change is here:
However, classes such as BrowserWindow in electron.d.ts try to extend EventEmitter so it is causing this issue.
May be related to #21475
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 34
- Comments: 81 (17 by maintainers)
Commits related to this issue
- feat(node): v13 (#40927) * feat(node): v13 * feat(node): v13.2 — committed to DefinitelyTyped/DefinitelyTyped by SimonSchick 5 years ago
- fix: compilation error with the latest @types/node this patch will fix https://github.com/electron/electron/issues/21612 — committed to rhysd/electron-typescript-definitions by rhysd 5 years ago
- fix: handle new EventEmitter type in node@13 type defs * fix: compilation error with the latest @types/node this patch will fix https://github.com/electron/electron/issues/21612 * apply suggest... — committed to electron/typescript-definitions by rhysd 4 years ago
- Allow GCS to use latest version of electron ## Why is the change being made? This change is made because GCS was running on electron v3, a version made back two years ago. This version is long depr... — committed to NGCP/GCS by Luis729 4 years ago
- Allow GCS to use latest version of electron ## Why is the change being made? This change is made because GCS was running on electron v3, a version made back two years ago. This version is long depr... — committed to NGCP/GCS by Luis729 4 years ago
- typescript: Fix errors hidden by skipLibCheck. This requires temporarily downgrading to @types/node@^12 (see https://github.com/electron/electron/issues/21612). Leave skipLibCheck on for now as it s... — committed to zulip/zulip-desktop by andersk 4 years ago
- 添加了@types/node 依赖以解决 https://github.com/electron/electron/issues/21612 — committed to SartreShao/timebarrier.electron.typescript by SartreShao 4 years ago
- Add @types/node@12 as a workaround for a console warning (see https://github.com/electron/electron/issues/21612#issuecomment-596155306) Change format in main/index.ts — committed to StreamerBuddy/streamerbuddy by aheidelberg 4 years ago
- fix compilation error see: https://github.com/electron/electron/issues/21612#issuecomment-632641479 — committed to kei10in/electron-webpack-typescript-react-redux-toolkit-quick-start by kei10in 4 years ago
- npm i -D @types/node@12 1674:31 Cannot extend an interface 'NodeJS.EventEmitter'. Did you mean 'implements'? 1672 | } 1673 | > 1674 | class BrowserWindow extends NodeJS.EventEmitter { use node 12 ht... — committed to jks-liu/sipi by jks-liu 4 years ago
- chore: npm i -DE @type/node@12 https://github.com/electron/electron/issues/21612 — committed to namikingsoft/interv-timer by namikingsoft 4 years ago
Why is this closed? This still doesn’t work
@marcschroeder If you don’t need Node 13 typings, you can add @types/node@12 to your dependencies.
e.g.
npm i --save-dev @types/node@12oryarn add -D @types/node@12Run
npm install --save --save-exact @types/node@^12.12.6. It works afterward (even with Electron v8.2.5).As it is said repeatedly in this issue thread, the fix for electron.d.ts for Node v13 is already there. I had made a PR and it was merged. But Node.js integrated to Electron is still v12 even in Electron v9. So using
@types/node@12is proper and the fix is not included in Electron v9. You should use@types/node@12.The
@types/nodefor node 13 also result in the following type error:More
https://github.com/DefinitelyTyped/DefinitelyTyped/commit/c47a34ead1637f6f34e7d630dc88ea3f6e5562cb#r36559684
Same issue for me when using instructions from : https://www.electronforge.io/templates/typescript-template
I was able to work around this for now by doing the follow in the initialized directory:
npm install @types/node --savethen editing package.json changing
"dependencies": { "@types/node": "^13.1.4", "electron-squirrel-startup": "^1.0.0" }to
"dependencies": { "@types/node": "12.12.6", "electron-squirrel-startup": "^1.0.0" }and finally running
npm install && npm startIt’s expected as Electron v7 comes with node v12, so use the most recent @types/node v12.
Could a maintainer of Electron (or someone else who knows) maybe clarify what the release cycle of type definitions looks like? There were multiple releases for Eletron 8.x in the past weeks, none of which shipped with the updated definitions. Does it mean they will ship with Electron 9 (is this considered a breaking change)? Or is this update dependent on something else?
@j-o-d-o If your using yarn, add to
package.jsonthis:Otherwise, it might resolve version 13 implicitly by some deps
This workaround works! Thanks! Minor correction: the
@types/nodeversion inpackage.jsonshould be12.12.21instead of12.21.12.same problem even with electron v9.0.0
Workaround:
npm install"@types/node":Of course it works this way. The point of this issue is that other deps used in the project require node@^13.
Force all references in your package.json and yarn.lock to use @types/node 12.21.12. This is an unacceptable but hopefully temporary scenario
There’s a few people trying to act as though the problem is electron 7 uses node 12 and not 13. They’re just plain wrong. The problem is the use of extends vs implements keywords on classes/interfaces. Again you have version ranges (for example, redux says @types/node:^8)
On Wed, Dec 25, 2019, 8:33 AM Cyrus Frost notifications@github.com wrote:
I still have this problem. I can’t keep my @types/node to 12.12.21 for long time. could you please reopen the issue and fix it?
Fine, mea culpa - it’s not your fault. Nevertheless, the attempt to resolve this issue by the electron team is woefully inadequate.
It seems that what happened is that
electron@8.0 now specifies specifically@types/node12 in itspackage json- but otherwise makes no attempt to useimplementsas opposed toextends(which would have worked for@types/node12 or 13) , and consequentlyyarnmade anode_modules/@types/nodedirectory underneath thenode_modules/electrondirectory. The problem is if some other library called for@types/node13 or * (which brings in ^13), typescript only uses one of the two folders (not both).I ended up with the following folder structure and at compile time typescript only accessed one
@types/node(the root one which is 13.x)Downgrading to version
12.12.6helped.Looks like we got thoughtful TypeSript and semver versioning expert here ==> @amirburbea
Electron npm module is shipped with @types/node@^12.0.12 at the moment so it’s totally fine that they do
extends NodeJS.EventEmittersinceNodeJS.EventEmitteris a class in @types/node prior v13.Electron even has automated test added for verifying that major versions of bundled node and @types/node got matched, see https://github.com/electron/electron/blob/f426ad1b5914295f9684036fb55f918c1b074900/spec-main/types-spec.ts
For curious persons, checking @types/node and nodejs versions correlation:
You can clone the https://github.com/DefinitelyTyped/DefinitelyTyped and execute
git log -L 1,1:types/node/index.d.ts --pretty=onelineto see that commit message corresponds to first-line/header of thetypes/node/index.d.tsfile change. So this pretty much indicates that version of @types/node corresponds to nodejs version.Regarding @types/node v13. You could see that what they do there with like 160 d.ts files of different libraries to make them compatible with @types/node@^13 is replacing
with
So @electron will probably follow the same way one day when they move to @types/node@^13.
CC @MarshallOfSound
guys this is closed issue and nobody will look at it. I oped another issue please add your comments on that.
I’ve got another, pre-2.9-compatible one at electron/typescript-definitions#164, but I would only use it if old Typescript compatibility is very important. As I commented on electron/typescript-definitions#163, the numbers we see from VS and VS Code indicate that it’s probably not.
Any ideas anyone?
I have raised a pull request at https://github.com/electron/typescript-definitions/pull/163 for this issue.
Thank you for your support @andrewbranch! I finally could fix
electron.d.tsas follows without breaking change. I confirmedtsccompiled successfully.https://gist.github.com/rhysd/c8d68f0399a84a5383aee67857642852
I will try to fix auto-generation logic.
Sorry, I should have tried the code I said before saying it. This is what I meant @rhysd:
However, note that this makes
EventEmitterappear global, which may not be desirable. I thinkdeclare globalis a better option all around.I was mistaken—I realized that
eventsis globally available in node at runtime, like other built-in modules, and assumed it had a type declaration, but it does not.I think it’s safe to move to 3.0 as a baseline for electron’s use of typescript. That would be 18 months old already. (TS3.0 released July2018, TS 2.8 was March2018 - not even a huge lag time between them)