typescript - How write generic interface which maps to properties only with selected types of T? -
lets have user object:
export class user { @column() public email: string; @column({ nullable: true }) public name: string; @column({ nullable: true }) public birthdate: date; @onetomany(type => article, article => article.author) public articles: promise<article[]>; }
i can create partial selection type information following interface:
export interface seed<t> { rows: {[k in keyof t]?: t[k]}[]; }
the problem following: approach see relations of entity have make key selection of class partial accept seeds like:
const user_seed: seed<user> = { email: test@mail.com, name: 'johny test', birthdate: new date('1990-01-08'), }
this can cause incorrect seeds not nullable field not provided or field provided not exists in table because it's relation.
my question how can create selection of keys selected types? in case select keys type of string, number , date
you're looking pick
, accepts key type:
pick<{a: number, b: boolean, c: string}, 'a' | 'b'> ==> {a: number, b: boolean}
you can't select keys types though.
Comments
Post a Comment