In my "models" registry, whenever I select a model and invoke a method on it, TypeScript anticipates an intersection of parameters across all registered models.
To demonstrate this issue concisely, I've created a dummy method called "getName".
export class Model<N extends string> {
public name: N;
constructor(name: N) {
this.name = name;
}
public getName = (options: { __type: N }) => options.__type;
}
export const Book = new Model("Book");
export const User = new Model("User");
export const modelRegistry = { Book, User };
export type ModelRegistry = typeof modelRegistry;
export const makeModel = <N extends keyof ModelRegistry>(name: N) => (
options: Parameters<ModelRegistry[N]["getName"]>[0],
) => {
const model = modelRegistry[name];
return model.getName(options); // <-- bug: TS expects this to be { __type: User } & { __type: Book }
};