typescript - How to define Object Key as String Type -
below function signature,
export function getvideobooleansparamsstring(videobooleans: { string: { label: string; value: boolean } }): string {} and arg trying pass into,
const videobooleans = { video: { label: 'video', value: true } } and produce following error,
(63,33): error ts2345: argument of type '{ exchange: { filtertype: string; filtervalues: string[]; }; }' not assignable parameter of type '{string: filtermodel; }'. property 'string' missing in type '{ exchange: { filtertype: string; filtervalues: string[]; }; }'. what intend declare object whatever key has content of { label: string; value: boolean }
the way have defined expects following:
const videobooleans = { string: { label: 'video', value: true } }; getvideobooleansparamsstring(videobooleans); you can define this, if want key string:
export function getvideobooleansparamsstring(videobooleans: { [key: string]: { label: string; value: boolean } }): string { } or this, more specific:
export function getvideobooleansparamsstring(videobooleans: { video: { label: string; value: boolean } }): string { } you can define interface, can make function signature bit cleaner:
interface myvideotype { [key: string]: { label: string, value: true }; } export function getvideobooleansparamsstring(videobooleans: myvideotype): string { } const video: myvideotype = { video: { label: 'video', value: true } }; getvideobooleansparamsstring(video);
Comments
Post a Comment