TypeScript-DOM-lib-generator
TypeScript-DOM-lib-generator copied to clipboard
Not all interfaces should be marked as constructable
E.g. trying to call AbortSignal with new throws Illegal constructor error.
> new AbortSignal()
Uncaught TypeError: Illegal constructor.
This is not currently handled by the declaration:
declare var AbortSignal: {
prototype: AbortSignal;
new(): AbortSignal;
};
This is because instanceof check requires a constructor member. But never seemingly works:
interface Foo {}
declare var Foo: {
prototype: Foo;
new(): never;
};
let foo = {} as Foo;
if (foo instanceof Foo) {
foo // $Foo
}
@orta does new(): never make sense to fix this?
We bounced the idea back and forth a bit, and generally think this is more likely to cause more breakages than it cures 👍🏻
cause more breakages than it cures
What breakages for example?
This also affects NodeList (which could have prevented a bug: https://github.com/mermaid-js/mermaid/pull/3396)
Maybe it's worth putting in a /** @deprecated */ JSDoc tag to constructors that throw Illegal constructor, so some tools (like ESLint/VS Code) will warn about using them.
Sounds good to me but I wonder what TS team thinks about the suggestion, maybe @DanielRosenwasser?
I just ran into this issue. Adding /** @deprecated */ seems like a good and simple fix.
What do you think, @DanielRosenwasser?