我应该在函数 createField
中做什么才能消除这个语法错误突出显示?
const createField = (
dataType,
options = {
required: false,
label: null,
min: Number.NEGATIVE_INFINITY,
max: Number.POSITIVE_INFINITY,
email: false,
match: false
}
) => {
return {
dataType,
options: {
...options,
label: options.label && options.label.toString() || null,
},
};
};
答案 0 :(得分:1)
这里有一个可能的解决方案:
interface CreateFieldOptions {
required?: boolean;
// you need to change the type here
label?: unknown;
min?: number;
max?: number;
email?: boolean;
match?: boolean;
}
const createFieldDefaultOptions: CreateFieldOptions = {
required: false,
label: null,
min: Number.NEGATIVE_INFINITY,
max: Number.POSITIVE_INFINITY,
email: false,
match: false
}
const createField = (
dataType,
options: CreateFieldOptions = {}
) => {
return {
dataType,
options: {
...createFieldDefaultOptions,
...options,
label: options.label && options.label.toString() || null,
},
};
};
有哪些变化?
CreateFieldOptions
createFieldDefaultOptions
options
参数的类型现在是可选的 CreateFieldOptions
options
属性的对象由 createFieldDefaultOptions
扩展
createFieldDefaultOptions
需要是第一个,以便它可以被提供的 options