Generic type inference broken when using | anywhere in a class
NOTE: This is a different issue to #5052, which was a spurious example based on stripping down the example code too much.
The below code fails to compile because the Schema type is not correctly inferred. The root cause is this declaration in the BaseData
data(in_newData: Schema[] | Array<{}>): void;
If I remove that method, or change it to only take Schema[], then everything compiles OK.
Example Code:
declare class BaseData<Schema> {
default(): Schema;
data(in_newData: Schema[] | Array<{}>): void;
}
declare class Widget<Schema> {
constructor(data: BaseData<Schema>);
default(): Schema;
}
declare interface DataSchema {
name: string;
}
declare class ExtendedData extends BaseData<DataSchema> { }
function test() {
let widgetWithExtendedData = new Widget(new ExtendedData());
// Compile error. Intellisense shows widgetWithExtendedData as type Widget<{}>
console.log(widgetWithExtendedData.default().name);
}
Follow up. I can also fix the compile error by changing the BaseData declaration to the following:
declare class BaseData<Schema> {
default(): Schema;
data(in_newData: Schema[]): void;
data(in_newData: Array<{}>): void;
}
The issue arises when trying to infer from the type DataSchema[] | {}[] to the type Schema[] | {}[]. Union type inference relates each source type to each target type, so we end up inferring that Schema might be {}, which indeed is true given the way you're written the types.
I'm wondering why you're writing Schema[] | {}[] in the first place? You might as well just write {}[] since any array type is assignable to {}[]. That should solve the problem.
Hi @ahejlsberg, here is a more complete example. Apologies for the slow response, I have been extracting this example code from a large code base... it was tricky to isolate exactly what real world combination was triggering the issue.
declare interface Event {
}
declare class EventCollection {
push(...events: Event[]): number;
}
declare class ModelArray<Schema> extends EventCollection {
toJSON(): Schema[];
}
declare class BaseData<Schema> {
default(): Schema;
data(in_newData: Schema[] | ModelArray<Schema>): void;
}
declare class Widget<Schema> {
constructor(data: BaseData<Schema>);
default(): Schema;
}
declare interface DataSchema {
name: string;
}
declare class ExtendedData extends BaseData<DataSchema> { }
function test() {
let widgetWithExtendedData = new Widget(new ExtendedData());
// Compile error. Intellisense shows widgetWithExtendedData as type Widget<Event>
console.log(widgetWithExtendedData.default().name);
}
I think the problem is that the structural typing is inferring that EventCollection is an Array
Thinking about this there are probably any number of complex cases where there is an ambiguous choice for a generic type based on structural typing alone. In the case above (which at least in our codebase is very common) there is a simple solution... walk the class hierarchy first to see if that yields a match for generic type, and only if no match is found go to structural typing. You mentioned that already if there is an exact type match you infer the generic type directly rather than going to structural typing... so this is just an extension of that. For example if I had
declare class Base<Schema> {}
declare class Derived<Schema> extends Base<Schema> {}
declare class Concrete extends Derived<MyCustomSchema> {}
function Process<Schema>(data:Base<Schema>) {
}
Process(new Concrete());
We would walk the hierarchy based on the known type of the data param for Process
- Concrete matches Base<Schema>? No, go to parent
- Derived<MyCustomSchema> matches Base<Schema>? No, go to parent
- Base<MyCustomSchema> matches Base<Schema>? Yes, generic type is MyCustomSchema
Of course you would have to traverse the interface hierarchy as well, but I think all up this would result in much more robust generic type inference compared to relying on structural typing alone.