TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

Not all interfaces should be marked as constructable

Open pavloconuve opened this issue 4 years ago • 7 comments
trafficstars

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;
};

pavloconuve avatar Oct 01 '21 13:10 pavloconuve

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?

saschanaz avatar Oct 02 '21 13:10 saschanaz

We bounced the idea back and forth a bit, and generally think this is more likely to cause more breakages than it cures 👍🏻

orta avatar Oct 04 '21 21:10 orta

cause more breakages than it cures

What breakages for example?

saschanaz avatar Oct 04 '21 21:10 saschanaz

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.

aloisklink avatar Sep 04 '22 22:09 aloisklink

Sounds good to me but I wonder what TS team thinks about the suggestion, maybe @DanielRosenwasser?

saschanaz avatar Sep 04 '22 22:09 saschanaz

I just ran into this issue. Adding /** @deprecated */ seems like a good and simple fix.

What do you think, @DanielRosenwasser?

Skalman avatar May 24 '24 19:05 Skalman