I have the following implementation
export class DbService<T extends object> {
addOne<TKey extends keyof T>(table: TKey, object: T[TKey]) {
return this.db.table(String(table)).add(object);
}
}
It works fine when you give a valid interface as T:
interface DbSchema {
users: User
}
var db = new DbService<DbSchema>();
// working fine here.
// It asks for 'users' key that matches the DbSchema
// And asks for User object implementation
db.addOne('users', { name: 'foo' });
But now I wish that DbSchema interface could be optional and you could give any key as table. So I tried the following overload method:
addOne<TKey extends keyof T>(table: TKey, object: T[TKey]): T[TKey];
addOne<O extends object>(table: string, object: O): O;
addOne(table: any, object: any) {
return this.db.table(String(table)).add(object);
}
I can say that it partially worked, but typescript stopped from help me with the object implementation even when the key matches with the DbSchema.
It starts well, showing keys from DbSchema:
But when you start to write the object it loses the track and jumps to second overload:
Is there anything I can do to help it?


