Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何理解Typescript错误消息?_Typescript - Fatal编程技术网

如何理解Typescript错误消息?

如何理解Typescript错误消息?,typescript,Typescript,我对Typescript还不熟悉,我正努力想弄清楚Typescript会吐出的错误消息。我花了数小时试图搜索以下内容: ERROR in [at-loader] ./src/Radio/styles.tsx:142:3 TS2322: Type 'StyledComponent<"input", WireframeTheme, {}, never>' is not assignable to type 'StyledComponent<"input", Wirefram

我对Typescript还不熟悉,我正努力想弄清楚Typescript会吐出的错误消息。我花了数小时试图搜索以下内容:

ERROR in [at-loader] ./src/Radio/styles.tsx:142:3
    TS2322: Type 'StyledComponent<"input", WireframeTheme, {}, never>' is not assignable to type 'StyledComponent<"input", WireframeTheme, RadioStyleProps, never>'.
  Type 'StyledComponent<"input", WireframeTheme, {}, never>' is not assignable to type 'StyledComponentBase<"input", WireframeTheme, RadioStyleProps, never>'.
    Types of property 'defaultProps' are incompatible.
      Type 'Partial<Pick<Pick<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "children" | ... 284 more ... | "step"> & { ...; }, "children" | ... 285 more ... | "step"> & Partial<...>, "children" | ... 285 more ... | "step"> & { ...; }> | undefined' is not assignable to type 'Partial<Pick<Pick<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "children" | ... 284 more ... | "step"> & { ...; } & RadioStyleProps, "children" | ... 287 more ... | "hasIcon"> & Partial<...>, "children" | ... 287 more ... | "hasIcon"> & { ...; }> | undefined'.
        Type 'Partial<Pick<Pick<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "children" | ... 284 more ... | "step"> & { ...; }, "children" | ... 285 more ... | "step"> & Partial<...>, "children" | ... 285 more ... | "step"> & { ...; }>' is not assignable to type 'Partial<Pick<Pick<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "children" | ... 284 more ... | "step"> & { ...; } & RadioStyleProps, "children" | ... 287 more ... | "hasIcon"> & Partial<...>, "children" | ... 287 more ... | "hasIcon"> & { ...; }>'.
          Types of property 'size' are incompatible.
            Type 'number | undefined' is not assignable to type 'undefined'.
              Type 'number' is not assignable to type 'undefined'.
[at loader]中出现错误。/src/Radio/styles.tsx:142:3 TS2322:类型“StyledComponent”不可分配给类型“StyledComponent”。 类型“StyledComponent”不可分配给类型“StyledComponentBase”。 属性“defaultProps”的类型不兼容。 类型“Partial | undefined”不可分配给类型“Partial | undefined”。 类型“Partial”不可分配给类型“Partial”。 属性“size”的类型不兼容。 类型“number | undefined”不可分配给类型“undefined”。 类型“number”不可分配给类型“undefined”。 据我所知,似乎存在类型不匹配。但我不知道是哪一部分

任何帮助都将不胜感激


谢谢

因此,您正在某处将名为
size
的属性设置为类型
number | undefined
。然后,其他地方正试图使用该属性,但它无法实现一个接口,该接口指定
size
只能是
number

无论您在哪个对象中设置该属性,都会在其他地方使用,然后在其他地方使用,依此类推。这就是为什么您会看到这么长的类型错误列表

考虑这个简单的例子,它抛出了一个类型错误

interface SomeType {
  size: number,
}

const MyComponent: React.FC = () => {
  let someSetting = "small";

  const getSize = () => {
    if (someSetting === "large") {
      return 100;
    }
  }

  let myVar: SomeType = {
    size: getSize(), // This line has a type error.
  };

  return null;
}
类型
SomeType
表示大小必须始终是一个数字,而不是其他。我的组件试图创建一个名为
myVar
的变量,该变量必须实现SomeType接口。为了设置
size
,我调用函数
getSize
,在一种情况下返回100,但在另一种情况下未定义。这会导致类型错误。当我向getSize函数添加一个类型时,原因变得更加明显,如下所示:

const getSize = (): number | undefined => {
  if (someSetting === "large") {
    return 100;
  }
}
现在您可以清楚地看到函数返回
number | undefined
。Typescript正在报告错误:

Type 'number | undefined' is not assignable to type 'number'.
  Type 'undefined' is not assignable to type 'number'.ts(2322)
App.tsx(17, 3): The expected type comes from property 'size' which is declared here on type 'SomeType'
要解决这个问题,我只需要跟踪将属性设置为错误类型的位置,然后确保始终正确设置类型。下面是我如何修复getSize()方法,使其始终返回一个数字:

const getSize = () => {
  if (someSetting === "large") {
    return 100;
  } else {
    return 50;
  }
}

现在,
getSize()
函数无法返回除数字以外的任何内容,我已解决了类型错误。

因此,您正在某处将名为
size
的属性设置为类型
number |未定义的
。然后,其他地方正试图使用该属性,但它无法实现一个接口,该接口指定
size
只能是
number

interface SomeType {
  size: number,
}

const MyComponent: React.FC = () => {
  let someSetting = "small";

  const getSize = () => {
    if (someSetting === "large") {
      return 100;
    }
  }

  let myVar: SomeType = {
    size: getSize(), // This line has a type error.
  };

  return null;
}
无论您在哪个对象中设置该属性,都会在其他地方使用,然后在其他地方使用,依此类推。这就是为什么您会看到这么长的类型错误列表

考虑这个简单的例子,它抛出了一个类型错误

interface SomeType {
  size: number,
}

const MyComponent: React.FC = () => {
  let someSetting = "small";

  const getSize = () => {
    if (someSetting === "large") {
      return 100;
    }
  }

  let myVar: SomeType = {
    size: getSize(), // This line has a type error.
  };

  return null;
}
类型
SomeType
表示大小必须始终是一个数字,而不是其他。我的组件试图创建一个名为
myVar
的变量,该变量必须实现SomeType接口。为了设置
size
,我调用函数
getSize
,在一种情况下返回100,但在另一种情况下未定义。这会导致类型错误。当我向getSize函数添加一个类型时,原因变得更加明显,如下所示:

const getSize = (): number | undefined => {
  if (someSetting === "large") {
    return 100;
  }
}
现在您可以清楚地看到函数返回
number | undefined
。Typescript正在报告错误:

Type 'number | undefined' is not assignable to type 'number'.
  Type 'undefined' is not assignable to type 'number'.ts(2322)
App.tsx(17, 3): The expected type comes from property 'size' which is declared here on type 'SomeType'
要解决这个问题,我只需要跟踪将属性设置为错误类型的位置,然后确保始终正确设置类型。下面是我如何修复getSize()方法,使其始终返回一个数字:

const getSize = () => {
  if (someSetting === "large") {
    return 100;
  } else {
    return 50;
  }
}
现在,
getSize()
函数无法返回数字以外的任何内容,我已经解决了类型错误

interface SomeType {
  size: number,
}

const MyComponent: React.FC = () => {
  let someSetting = "small";

  const getSize = () => {
    if (someSetting === "large") {
      return 100;
    }
  }

  let myVar: SomeType = {
    size: getSize(), // This line has a type error.
  };

  return null;
}