Closed as not planned
Description
π Search Terms
class satisfies
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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:
type PrefixedWith$<T> = T & { [K in keyof T as `$${K & string}`]: T[K] };
class Foo satisfies PrefixedWith$ {
bar = 1;
constructor() {
for (const [key, value] of Object.entries(this)) {
Object.defineProperty(this, '$' + key, { value });
}
this.$bar // now we can use this in constructors
}
qux() {
this.$bar // and methods
}
}
const foo = new Foo();
foo.$bar // and externally
π» Use Cases
- What do you want to use this for?
Telling TypeScript about dynamically generated types known about statically.
- What shortcomings exist with current approaches?
You have to wrap the class constructor, and constantly cast this, either in method return value assertions or method this-parameters.
- What workarounds are you using in the meantime?
Giving up.