DEV Community

Discussion on: Things to watch for in TypeScript 4.2 onwards

Collapse
 
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

looks good. Do you think static indexing will obviate these kinds of errors?

class Foo {
  declare static type: "a"|"b";
}

class Bar {
  static type = "b";
}

const a = new Bar();

// @ts-expect-error: you're going to have to trust me on this one
if (a.constructor.type === 'a')
  console.log('oops! a compiler error!'); 
Enter fullscreen mode Exit fullscreen mode

This comes up a lot when duck-typing custom elements at run time.

Collapse
 
43081j profile image
James Garbutt

in that example it wouldn't be of much help, but IIRC there's ongoing work elsewhere to type constructor as typeof T. if it was, rather than Function, it would work in your example i think