I have a Model
constructor that looks like this:
<code>interface Schema {
foo: string;
bar: string;
}
class Model<T extends Schema = Schema> {}
</code>
<code>interface Schema {
foo: string;
bar: string;
}
class Model<T extends Schema = Schema> {}
</code>
interface Schema {
foo: string;
bar: string;
}
class Model<T extends Schema = Schema> {}
I am implementing it to create models as:
<code>export interface ThingOneSchema extends Schema {
baz: string;
qux: string;
}
const ThingOne = new Model<ThingOneSchema>({ ... });
</code>
<code>export interface ThingOneSchema extends Schema {
baz: string;
qux: string;
}
const ThingOne = new Model<ThingOneSchema>({ ... });
</code>
export interface ThingOneSchema extends Schema {
baz: string;
qux: string;
}
const ThingOne = new Model<ThingOneSchema>({ ... });
<code>export interface ThingTwoSchema extends Schema {
bam: string;
boom: string;
}
const ThingTwo = new Model<ThingTwoSchema>({ ... });
</code>
<code>export interface ThingTwoSchema extends Schema {
bam: string;
boom: string;
}
const ThingTwo = new Model<ThingTwoSchema>({ ... });
</code>
export interface ThingTwoSchema extends Schema {
bam: string;
boom: string;
}
const ThingTwo = new Model<ThingTwoSchema>({ ... });
I then have an array of multiple models, all the same class, but with different generics that always extend the base interface:
<code>const models: Model[] = [
ThingOne,
ThingTwo,
];
</code>
<code>const models: Model[] = [
ThingOne,
ThingTwo,
];
</code>
const models: Model[] = [
ThingOne,
ThingTwo,
];
However TS is complaining, and I’m not quite understanding why:
<code>Type `Schema` is not assignable to type `ThingOneSchema`.
Type `Schema` is not assignable to type `ThingTwoSchema`.
</code>
<code>Type `Schema` is not assignable to type `ThingOneSchema`.
Type `Schema` is not assignable to type `ThingTwoSchema`.
</code>
Type `Schema` is not assignable to type `ThingOneSchema`.
Type `Schema` is not assignable to type `ThingTwoSchema`.
Is there a better way that to type this array of class instances, or use some inference trick without using Model<any>
?