typescript-definitions
typescript-definitions copied to clipboard
Use `unknown` instead of `any`
unknown is the type-safe version of any and should be preferred.
https://stackoverflow.com/questions/51439843/typescript-3-0-unknown-vs-any
I believe unknown will break existing code and force people to do a whole bunch of if checks or any casting to access known properties.
E.g. remote.getGlobal() is currently typed as any, in an ideal world it would be typed
getGlobal<T>(name: string): T;
or even better
getGlobal<K extends keyof NodeJS.Global>(name: K): NodeJS.Global[K]
I've had vague discussions with @BinaryMuse about writing docs / types in typescript as the source of truth so we can be better with types which would solve this (we would type things correctly instead of using the docs parser). But I don't think we can just replace all any usages with unknown.
On a case-by-case basis we could probably make some things unknown in the current docs files. Do you have any specific examples on which APIs should probably be unknown to enforce better type safety?
cc @felixrieseberg @bpasero @BinaryMuse You folks may have opinions here
I believe unknown will break existing code and force people to do a whole bunch of if checks or any casting to access known properties.
That's kind of the point. You shouldn't just access properties without asserting the type first. That completely loses the point of types, and can result in runtime errors that are not caught at compile-time.
getGlobal<K extends keyof NodeJS.Global>(name: K): NodeJS.Global[K]
👍
I've had vague discussions with @BinaryMuse about writing docs / types in typescript as the source of truth so we can be better with types which would solve this (we would type things correctly instead of using the docs parser). But I don't think we can just replace all any usages with unknown.
👍 Much better to generate docs from TS source than the current weird setup of generating types from docs. I guess you mean this: https://github.com/electron/electron/pull/16441?
On a case-by-case basis we could probably make some things unknown in the current docs files. Do you have any specific examples on which APIs should probably be unknown to enforce better type safety?
I can find specific examples where it would be the most beneficial, but honestly, all of it should be unknown instead of any.
I guess you mean this: electron/electron#16441?
That PR actually depends on our current TS generation tech. Although a step towards a hopeful TS future it won't change how we generate docs / typings.
I can find specific examples where it would be the most beneficial, but honestly, all of it should be unknown instead of any.
I'm on board with unknown for literally unknown types, my issue is that at the moment a lot of our any types could be typed if we could write some fun TS types but we can't so they aren't. So for now I don't want to blanket say everything should be unknown.
Better docs / types is something I'm always interested in and will keep unknown in mind in any spikes in the future for better typings / docs 😄
One thing I like to use is object over any if I know an object is returned with any kind of properties.