以下是功能签名
export function getVideoBooleansParamsString(videoBooleans: {
string: { label: string; value: boolean }
}): string {}
这是我试图传递的arg
,
const videoBooleans = { video: { label: 'video', value: true } }
它会产生以下错误,
(63,33): error TS2345: Argument of type '{ exchange: { filterType: string; filterValues: string[]; }; }' is not assignable to parameter of type '{string: FilterModel; }'.
Property 'string' is missing in type '{ exchange: { filterType: string; filterValues: string[]; }; }'.
我打算声明的是一个对象,无论密钥是{ label: string; value: boolean }
答案 0 :(得分:3)
现在定义它的方式需要以下内容:
const videoBooleans = { string: { label: 'video', value: true } };
getVideoBooleansParamsString(videoBooleans);
如果您希望密钥为任何字符串,则可以像这样定义:
export function getVideoBooleansParamsString(videoBooleans: {
[key: string]: { label: string; value: boolean }
}): string { }
或者像这样,更具体一点:
export function getVideoBooleansParamsString(videoBooleans: {
video: { label: string; value: boolean }
}): string { }
您还可以定义一个接口,它可以使函数签名更清晰:
interface MyVideoType {
[key: string]: { label: string, value: true };
}
export function getVideoBooleansParamsString(videoBooleans: MyVideoType): string { }
const video: MyVideoType = { video: { label: 'video', value: true } };
getVideoBooleansParamsString(video);
答案 1 :(得分:1)
您需要为参数类型定义索引:
export function getVideoBooleansParamsString(videoBooleans: {
[name :string] : { label: string; value: boolean }
}): string {}