-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow type-level extraction of a class name as a string literal #43325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I doubt they will think this is a bug; the Is this a dupe of #1579 maybe? |
Oh, that would be a much stronger feature, assuming const foo = "foobar";
foo // Type is "foobar" because `foo` was defined to be const. |
#4628 and #3841 highlight some of the issues surrounding the static side of classes. With the notable exception of the construct signature itself, the static side of a class is inherited by subclasses, and thus forms a type hierachy: class Foo {
static someStaticProp = "Foo";
a = 1;
}
class Bar extends Foo {
b = 2;
}
Bar.someStaticProp; // exists
let fooCtor: typeof Foo = Bar; // okay
class Baz extends Foo {
// construct signature incompatible with Foo
constructor(public c: number) {
super()
};
}
fooCtor = Baz; // error If the At least the |
Bug Report
Because class names cannot be modified and are known at compile time, they should be more narrowly typed.
🔎 Search Terms
🕗 Version & Regression Information
This behavior exists on Nightly (i.e. 4.3.0-dev.20210316) and the current version (4.2.3). It exists as far back as the compiler does - I was thereby not sure whether to classify this as a "bug" or a "feature request".
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
FooName
is of typestring
🙂 Expected behavior
I would expect
FooName
to be inferred as type"Foobar"
, since the name cannot change (as per itsreadonly
status).A simple use-case would be a string-to-constructor record, but my use-case is actually generating sort of custom compiler error messages using string template literals. Right now I'm using the workaround of having a
name
attribute on each class, but it would be nice to not require that for my end-users.The text was updated successfully, but these errors were encountered: