简体   繁体   English

VSCode 如何修复 typescript 突出显示错误 {} 类型的参数不可分配给

[英]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?有哪些变化?

  • Add an interface to manage the options type -> CreateFieldOptions添加管理选项类型的接口 -> CreateFieldOptions
    • So it can be reused所以可以重复使用
  • Extract the default options into a separate variable -> createFieldDefaultOptions将默认选项提取到单独的变量中 -> createFieldDefaultOptions
    • Your intent seems to be to merge the default options with the provided passed options.您的意图似乎是将默认选项与提供的传递选项合并。 But that is not possible via the parameter assignment, so it has to be done later (see last bullet point)但这无法通过参数分配实现,因此必须稍后完成(参见最后一个要点)
  • The type of the options parameter is now an optional CreateFieldOptions options参数的类型现在是可选的CreateFieldOptions
  • The returned object with the options attribute is extended by the createFieldDefaultOptions返回的带有options属性的 object 由createFieldDefaultOptions扩展
    • The order is important!顺序很重要! 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.

相关问题 Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' 参数无法分配给参数-打字稿错误 - Argument is not assignable to parameter - typescript error React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type 打字稿错误:“对象”类型的参数无法分配给“ {} []”类型的参数 - Typescript error: Argument of type 'Object' is not assignable to parameter of type '{}[]' 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error Typescript 枚举键入错误(“字符串”类型的参数不可分配给类型参数) - Typescript enums typing error (argument of type 'string' is not assignable to parameter of type) 如何解决“'number' 类型的参数不能分配给'string | RegExp' 类型的参数。” - How to fix "Argument of type 'number' is not assignable to parameter of type 'string | RegExp'." 如何修复“数字”类型的参数不可分配给“字符串”类型的参数 | 正则表达式'' - How to fix ' Argument of type 'number' is not assignable to parameter of type 'string | RegExp' ' Typescript:a 类型的参数不能分配给 b 类型的参数 - Typescript: argument of type a is not assignable to parameter of type b 如何解决 - 类型参数不可分配给类型 - TYPESCRIPT - How to resolve - Argument of type is not assignable to type - TYPESCRIPT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM