One of the unique features of my type is that it includes a property which is a class taking that same type as a parameter.
interface CustomType<P extends {}> {
name: string;
props: P;
field: new(type: CustomType<P>) => { ... };
}
// Defining various Types
interface AllTypes {
Foo: CustomType<{}>;
Bar: CustomType<{
foo: string;
bar: number
}>
}
type SpecificType = AllTypes[keyof AllTypes];
I have a function that receives a SpecificType
and attempts to create an instance of its field
, but it results in an error.
function myFunction(type: SpecificType) {
return new type.field(type); // Error
}
To fix this error, I could modify CustomType.field
to
new(type: SpecificType) => ...
. However, doing so may lead to additional errors later on.
class SpecialField<T extends SpecificType> {
type: T;
constructor(type: T) {
this.type = type;
}
}
declare class FooField extends SpecialField<AllTypes['Foo']> {}
declare class BarField extends SpecialField<AllTypes['Bar']> {}
const fields: SpecificType[] = [
{
name: 'foo',
props: {},
field: FooField,
},
{
name: 'bar',
props: { // This error is intentional and expected
foo: 0,
bar: 'hello',
},
field: BarField, // Error
},
];