I want to create a custom type or an interface in typescript which depends on the keys of another interface.
Initial Interface :
interface IModelCollection
{
Account: typeof Account;
Session: typeof Session;
...
}
I want to create another type which will be :
interface ISchemaCollection
{
Account: ReturnModelType<typeof Account>;
Session: ReturnModelType<typeof Session>;
...
}
I've tried the following but I get an error Type 'T[Key]' does not satisfy the constraint 'AnyParamConstructor<any>.
type TSschemaCollection<T extends object> =
{
[Key in keyof T]: ReturnModelType<T[Key]>;
}
Try this out.
type ReturnModelType<T> = any; // Define ReturnModelType based on your requirements
interface ISchemaCollection {
Account: typeof Account;
Session: typeof Session;
// ...
}
type TSschemaCollection<T extends ISchemaCollection> = {
[Key in keyof T]: ReturnModelType<T[Key]>;
};
Edit Update your models and see if this helps.
public get Models(): { [Key in keyof T]: ReturnModelType<T[Key]> } {
const models = this._models as { [Key in keyof T]?: ReturnModelType<T[Key]> };
return Object.fromEntries(
Object.entries(models).map(([key, model]) => [
key as keyof T,
getModelForClass(model as any)
])
);
}