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
Using decorators and proxies it's now possible to elegantly create classes which don't require the new keyword for instantiation. Unfortunately TypeScript doesn't allow this.
typeCallable<Textendsnew(...args: any)=>any>=T&((...args: ConstructorParameters<T>)=>InstanceType<T>);functionmakeCallable<Cextendsnew(...args: any)=>any>(clazz: C): Callable<C>{returnnewProxy(clazz,{apply(target,thisArg,argArray){returnnewtarget(...argArray);},})asunknownasCallable<C>;}functioncallable<Cextendsnew(...args: any)=>any>(clazz: C,context: ClassDecoratorContext<C>): Callable<C>{returnmakeCallable(clazz);}// Using functionconstUser=makeCallable(class{constructor(name: string,age?: number){}});constx=newUser('foo');console.log(xinstanceofUser);consty=User('bar');// worksconsole.log(yinstanceofUser);// Using decorator
@callableclassDecoratedUser{constructor(name: string,age?: number){}}consta=newDecoratedUser('foo',19);console.log(ainstanceofDecoratedUser);constb=DecoratedUser('bar',12);// Value of type 'typeof DecoratedUser' is not callable.console.log(binstanceofDecoratedUser);
🙁 Actual behavior
TypeScript does not allow for the proxied class to be called, although this is valid js code and all instanceof checks are passing.
🙂 Expected behavior
TypeScript should use the return type of the decorator as the new type for the class.
The text was updated successfully, but these errors were encountered:
Bug Report
Using decorators and proxies it's now possible to elegantly create classes which don't require the
new
keyword for instantiation. Unfortunately TypeScript doesn't allow this.🔎 Search Terms
decorators, class decorators
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
TypeScript does not allow for the proxied class to be called, although this is valid js code and all instanceof checks are passing.
🙂 Expected behavior
TypeScript should use the return type of the decorator as the new type for the class.
The text was updated successfully, but these errors were encountered: