You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classFoo{}typeFooName=typeofFoo.constructor.name;// Should be literal `"Foo"`, but is instead `string`
In the context of this issue, I am not sure whether this is a "bug" or a "suggestion", but I went with a suggestion for now.
π Motivating Example
I want to use TypeScript to ensure that all of my classes meet some criteria. Consider the following trivial example:
classFoo{}classBar{}classBaz{}/** We organize them into an array so that we can instantiate them all at the same time with some common parameters. */constMY_CLASSES=[Foo,Bar,Baz,]asconst;/** Helper type to retrieve the class names of a tuple containing classes. */typeMyClassNames=InstanceType<typeofMY_CLASSES[number]>["constructor"]["name"]// Oops! TypeScript messed up, and `MyClassName` is `string` instead of `"Foo" | "Bar" | "Baz"`.constCLASSES_TO_METADATA={Foo: "something",Bar: "something else",}asconstsatisfiesRecord<MyClassNames,string>
Oops, I forgot to put an entry for my Baz class inside of my CLASSES_TO_METADATA object. Even though I carefully took the time to specify a satisfies constraint, TypeScript doesn't care!
I do not think there are any workarounds for this "bug" right now other than to explicitly duplicate the name of the class inside of the class itself, like this:
classFoo{readonlyname="Foo"asconst;}classBar{readonlyname="Bar"asconst;}classBaz{readonlyname="Baz"asconst;}/** We organize them into an array so that we can instantiate them all at the same time with some common parameters. */constMY_CLASSES=[Foo,Bar,Baz,]asconst;/** Helper type to retrieve the class names of a tuple containing classes. */typeMyClassNames=InstanceType<typeofMY_CLASSES[number]>["name"]constCLASSES_TO_METADATA={Foo: "something",Bar: "something else",}asconstsatisfiesRecord<MyClassNames,string>
Very ugly! But in this second code snippet, the satisfies part works properly.
The text was updated successfully, but these errors were encountered:
Trying to narrow via constructor.name is a fairly bad choice. The class may be subclassed, in which case the name will be different, but it will still be a valid instance.
classBase{}classDerivedextendsBase{}constbase: Base=newDerived();console.log(base.constructor.name);// base is typed Base, but logs "Derived"
Ok. Forgetting constructor.name for a moment, is there a way to go from Foo to the name of the method, purely at compile-time, via some other method? I'm not interested in any run-time behavior, only compile-time.
Suggestion
π Search Terms
class
constructor.name
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
In the context of this issue, I am not sure whether this is a "bug" or a "suggestion", but I went with a suggestion for now.
π Motivating Example
I want to use TypeScript to ensure that all of my classes meet some criteria. Consider the following trivial example:
Oops, I forgot to put an entry for my
Baz
class inside of myCLASSES_TO_METADATA
object. Even though I carefully took the time to specify asatisfies
constraint, TypeScript doesn't care!I do not think there are any workarounds for this "bug" right now other than to explicitly duplicate the name of the class inside of the class itself, like this:
Very ugly! But in this second code snippet, the
satisfies
part works properly.The text was updated successfully, but these errors were encountered: