-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Class satisfies type #61324
Comments
Effectively #49509. The use case is allowing a class statement (or interface) to have dynamic keys. I don't think |
@jcalz similar but not the same feature. That one maps over some type. This one maps over this type. |
So then that one is effectively a prerequisite for this one, given that the circularity of |
Sure I dunno π€· |
The problem is that this is apparently circular; it implies the existence of In cases where this isn't circular, it's already possible: type PrefixedWith$<T> = T & { [K in keyof T as `$${K & string}`]: T[K] };
class Foo {
bar = 1;
constructor() {
for (const [key, value] of Object.entries(this)) {
Object.defineProperty(this, '$' + key, { value });
}
}
}
class BarWith$ extends Foo {
qux() {
this.$bar // and methods
}
}
interface BarWith$ extends PrefixedWith$<Foo> { }
const foo = new BarWith$();
foo.$bar // ok |
Cool. But that requires a ton of boilerplate for every class and subclass, and is super error prone. |
Also, the real world example I was using excludes $$$$bar via never when extends |
I just want to do crazy magic in JavaScript and have TypeScript read my mind is that too much to ask π |
π Search Terms
class satisfies
β Viability Checklist
β Suggestion
Make the keyword
satisfies
modify the interface of a class when used after the class header.π Motivating Example
We can add properties at runtime in a way that can statically be known about and well typed.
But currently there's no way to tell TypeScript about these properties. This feature request would solve that:
π» Use Cases
Telling TypeScript about dynamically generated types known about statically.
You have to wrap the class constructor, and constantly cast this, either in method return value assertions or method this-parameters.
Giving up.
The text was updated successfully, but these errors were encountered: