[英]VSCode how to fix typescript highlight error Argument of type {} is not assignable to parameter of
What should I do in the function createField
in order to remove this syntax error highlighting?我应该在 function
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,
},
};
};
Here a possible solution:这是一个可能的解决方案:
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,
},
};
};
What are the changes?有哪些变化?
CreateFieldOptions
CreateFieldOptions
createFieldDefaultOptions
createFieldDefaultOptions
options
parameter is now an optional CreateFieldOptions
options
参数的类型现在是可选的CreateFieldOptions
options
attribute is extended by the createFieldDefaultOptions
options
属性的 object 由createFieldDefaultOptions
扩展
createFieldDefaultOptions
needs to be first, so that it can be overridden by the provided options
createFieldDefaultOptions
需要放在第一位,这样它就可以被提供的options
覆盖
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.