microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.22k stars 12.39k forks source link

Properly type `.constructor` for classes #51647

Closed hasezoey closed 1 year ago

hasezoey commented 1 year ago

lib Update Request

Configuration Check

My compilation target is es2019 and my lib is esnext.

Missing / Incorrect Definition

.constructor on class instances should return the proper typeof Class type instead of Object.constructor: Function

Sample Code

class Test {}

const instance = new Test();
instance.constructor; // constructor should have the type "typeof Test"
console.log(instance.constructor === Test);

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor


i would need this as a opposite of InstanceType<typeof Class>, for example type TypeofClass<T extends { constructor: new (...args: any[]) => any }> = T extends { constructor: infer S } ? S : never;

MartinJohns commented 1 year ago

Duplicate of #32452.

hasezoey commented 1 year ago

Duplicate of #32452.

weird, i tried searching for that, but didnt find it - though granted i did not search through all 14 (or with some terms more) pages

hasezoey commented 1 year ago

Duplicate of https://github.com/microsoft/TypeScript/issues/32452

MartinJohns commented 1 year ago

I used this search term: constructor type in:title