Search Terms
superclass base class generic type infer contextual infer parent generic super constructor
Suggestion
Make it possible to infer superclass type parameters / constructor overload from the super call
Use Cases
I'm working on an intermediate library with typings for a legacy codebase. That intermediate library is used in third party to use the functionality from the main codebase (called adapters). There are several supported ways to access this functionality:
const foo1 = Adapter(options);const foo2 = new Adapter(options);class Foo3 extends Adapter { ... } - The constructor must call the superclass constructor with the options.
Now the problem is that the Adapter "class" (actually an ES5-style "function" class) has some properties that only exist if specific options are passed to its constructor. I'm able to model this behavior for case 1 and 2, but not 3 - at least not without manually specifying the type.
Here's what I have so far. The code is split into three parts: main codebase, intermediate library, 3rd party / user code. (Playground link)
// legacy code, cannot change:declareclassInternalAdapter{// actually an ES5-style class function, can be called with and without new!someProp1: any;someProp2: any;// I want to override this typecacheObj: Record<string,any>|undefined;}// ========================// My code, can change:interfaceAdapterOptions{name: string;cache?: boolean;}typeAdapterInstance<TextendsAdapterOptions>=Textends{cache: true;}
? Omit<InternalAdapter,"cacheObj">&{cacheObj: Exclude<InternalAdapter["cacheObj"],undefined>;}
: Omit<InternalAdapter,"cacheObj">;interfaceAdapterConstructor{new(adapterName: string): AdapterInstance<{name: string}>;new<TextendsAdapterOptions>(adapterOptions: T): AdapterInstance<T>;(adapterName: string): AdapterInstance<{name: string}>;<TextendsAdapterOptions>(adapterOptions: T): AdapterInstance<T>;}declareconstAdapter: AdapterConstructor;// ========================// User code, should be as simple as possibleconstname="foobar";constoptions={ name };consttest1=Adapter(name);test1.cacheObj;// does not exist, expectedconsttest2=newAdapter(name);test2.cacheObj;// does not exist, expectedconsttest3=Adapter(options);test3.cacheObj;// does not exist, expectedconsttest4=newAdapter(options);test4.cacheObj;// does not exist, expectedconsttest5=newAdapter({ ...options,cache: true});test5.cacheObj;// exists, expected// Here, the problems start:classTest6extendsAdapter<AdapterOptions>{constructor(options: AdapterOptions){super(options);this.cacheObj;// does not exist, expected}}classTest7extendsAdapter<AdapterOptions>{constructor(options: AdapterOptions){super({ ...options,cache: true});this.cacheObj;// does not exist, unexpected}}classTest8extendsAdapter<AdapterOptions&{cache: true}>{constructor(options: AdapterOptions){super({ ...options,cache: true});this.cacheObj;// exists, but I have to duplicate the type}}Notice how the class definitions are all awkward. In Test6 I have to duplicate the generic type AdapterOptions or the super call will default to the string constructor.
In Test7 this is actually wrong, because the generic type overrides the conditional behavior for the cacheObj. This can be fixed like in Test8, but that is really ugly. IMO, TypeScript should be able to infer the type arguments from a constructor call if that type argument matches the class' type argument
Examples
Show how this would be used and what the behavior would be - Ideally it should be like this:
classTest9extendsAdapter{constructor(options: AdapterOptions){super({ ...options,cache: true});this.cacheObj;// should exist}}The super call would be used to infer that the 2nd constructor overload is the correct one:
new<TextendsAdapterOptions>(adapterOptions: T): AdapterInstance<T>;
and therefore the instance type would be AdapterInstance<AdapterOptions & {cache: true}>.
Checklist
My suggestion meets these guidelines:
Search Terms
superclass base class generic type infer contextual infer parent generic super constructor
Suggestion
Make it possible to infer superclass type parameters / constructor overload from the
supercallUse Cases
I'm working on an intermediate library with typings for a legacy codebase. That intermediate library is used in third party to use the functionality from the main codebase (called adapters). There are several supported ways to access this functionality:
const foo1 = Adapter(options);const foo2 = new Adapter(options);class Foo3 extends Adapter { ... }- The constructor must call the superclass constructor with the options.Now the problem is that the
Adapter"class" (actually an ES5-style "function" class) has some properties that only exist if specific options are passed to its constructor. I'm able to model this behavior for case 1 and 2, but not 3 - at least not without manually specifying the type.Here's what I have so far. The code is split into three parts: main codebase, intermediate library, 3rd party / user code. (Playground link)
Notice how the class definitions are all awkward. In
Test6I have to duplicate the generic typeAdapterOptionsor the super call will default to the string constructor.In
Test7this is actually wrong, because the generic type overrides the conditional behavior for thecacheObj. This can be fixed like inTest8, but that is really ugly. IMO, TypeScript should be able to infer the type arguments from a constructor call if that type argument matches the class' type argumentExamples
Show how this would be used and what the behavior would be - Ideally it should be like this:
The
supercall would be used to infer that the 2nd constructor overload is the correct one:and therefore the instance type would be
AdapterInstance<AdapterOptions & {cache: true}>.Checklist
My suggestion meets these guidelines: