For example, I have this code:
interface DbType {
id: string,
}
interface RowType extends DbType {
name: string
}
class db<T> { // How do I put that it must be an extension of DbType?
insert(item: T) { }
delete(id: [typeof id]) { } // What should I put here?
}
I want to limit the parameter type, also the T type, to be corresponds to DbType.
So when I do new db<RowType>() I want the delete method would accept the same type as DbType.id
How to do that?
>Solution :
I think what you’re looking for is a simple generic constraint and an Indexed Access type for the delete parameter.
interface DbType {
id: string;
}
interface RowType extends DbType {
name: string;
}
class DB<T extends DbType> {
insert(item: T) {}
delete(id: T["id"]) {}
}