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
But there's no way to change R if we call a member function of a class.
It's possible to make another workaround with this in a guard
classBuzz<RextendsObject={}>{publicadd<NextendsObject>(newValue: N): this is Buzz<R&N>{returntrue;}publicget<NextendskeyofR>(name: N): R[N]{return({}asunknown)asR[N];}}letbuzz=newBuzz();if(buzz.add({a: 123})){leta: number=buzz.get("a")// works fine}else{leta: number=buzz.get("a")// TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.}
but in this case we still need to use if condition that doesn't work
🔍 Search Terms
class generic
✅ Viability Checklist
My suggestion meets these guidelines:
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.)
It allows using the OOP approach without a need to create new objects.
💻 Use Cases
Live example. Dependency Injection container is getting new definitions. It is supposed to be a singleton in the program.
constcontainer=newDIContainer();container.add({key1: newFoo()});container.add({key2: newBar()});constvalue=container.get("key1");// currently I can only doletcontainer=newDIContainer();container=container.add({key1: newFoo()});container=container.add({key2: newBar()});
The text was updated successfully, but these errors were encountered:
This looks an awful lot like assertion functions as implemented in #32695 (although assertion functions have the annoying limitation that you need to explicitly annotate the class instance type in the variable that holds it) :
classBuzz<Textendsobject={}>{data: any={}add<Uextendsobject>(newValue: U): asserts this is Buzz<T&U>{Object.assign(this.data,newValue);}get<KextendskeyofT>(name: K): T[K]{returnthis.data[name]asT[K];}}letbuzz: Buzz=newBuzz();// explicitly annotated as Buzz here, or you get errorsbuzz.add({a: 123});buzz.get("b")// errorconsole.log(buzz.get("a").toFixed(2))// 123.00buzz.add({b: "string"});buzz.get("b")// okayconsole.log(buzz.get("b").toUpperCase());// STRING
Suggestion
Provide the ability to change assigned generic inside a class -
class Buzz<R>
Currently, it's possible to change class generic only by creating a new object.
But there's no way to change R if we call a member function of a class.
It's possible to make another workaround with
this
in a guardbut in this case we still need to use
if
condition that doesn't work🔍 Search Terms
class generic
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
use
this as CurrentClass<NewGenericType>
syntax to allow changing assigned generic.📃 Motivating Example
It allows using the OOP approach without a need to create new objects.
💻 Use Cases
Live example. Dependency Injection container is getting new definitions. It is supposed to be a singleton in the program.
The text was updated successfully, but these errors were encountered: