简体   繁体   English

在反应 typescript 中,类型“字符串”不可分配给类型“从不”并且类型“未知”不可分配给类型“从不”错误

[英]Type 'string' is not assignable to type 'never' and Type 'unknown' is not assignable to type 'never' error in react typescript

I have a function in React Typescript我在 React Typescript 中有一个 function

const filter = (data: any) => {
    if (Array.isArray(data)) {
      const temp = data.reduce((r, v) => {
        v = filter(v);
        if (v !== "") r.push(v);
        return r;
      }, []);
      return temp.length ? temp : "";
    }
    if (data && typeof data === "object") {
      const temp = Object.entries(data).reduce((r, [k, v]) => {
        v = filter(v);
        if (v !== "") r.push([k, v]);
        return r;
      }, []);
      return temp.length ? Object.fromEntries(temp) : "";
    }
    return data;
  };

but here in line if (v.== "") r,push([k; v]);但这里符合if (v.== "") r,push([k; v]); I am getting error Type 'string' is not assignable to type 'never'.ts(2322) and Type 'unknown' is not assignable to type 'never'.ts(2322) in k and v respectively, but I am not getting any solution to fix these issues.我收到错误Type 'string' is not assignable to type 'never'.ts(2322)Type 'unknown' is not assignable to type 'never'.ts(2322)分别分配给kv中的类型'从不'.ts(2322),但我没有得到解决这些问题的任何解决方案。 I am new with Typescript.我是 Typescript 的新手。

Typescript is not able to figure out the correct type of r in your reduce, so it assumes it to be never[] . Typescript 无法在您的 reduce 中r的正确类型,因此假定它是never[] You can get around this by specifiyng the correct type yourself.您可以通过自己指定正确的类型来解决此问题。

const filter = (data: any) => {
  if (Array.isArray(data)) {
    const temp = data.reduce((r, v) => {
      v = filter(v);
      if (v !== "") r.push(v);
      return r;
    }, []);
    return temp.length ? temp : "";
  }
  if (data && typeof data === "object") {
    const temp = Object.entries(data).reduce((r, [k, v]) => {
      v = filter(v);
      if (v !== "") r.push([k, v]);
      return r;
    }, [] as [string, unknown][]);
    return temp.length ? Object.fromEntries(temp) : "";
  }
  return data;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React TypeScript:类型“string []”不可分配给类型“never []” - React TypeScript: Type 'string[]' is not assignable to type 'never[]' 类型“string[]”不可分配给类型“never[]”。 Typescript 错误 - Type 'string[]' is not assignable to type 'never[]'. Typescript Error Typescript 错误:: 类型“数字”不可分配给类型“从不” - Typescript error :: Type 'number' is not assignable to type 'never' 类型 'string[]' 不能分配给类型 'never[]' - Type 'string[]' is not assignable to type 'never[]' reactjs + typescript 获取错误:类型“字符串”不可分配给类型“从不” - reactjs + typescript getting error: Type 'string' is not assignable to type 'never' React TypeScript:参数不可分配给“从不”类型的参数 - React TypeScript: Argument is not assignable to parameter of type 'never' 打字稿:'{translationSentences:从不[]; }' 不可分配给类型为 'never' 的参数 - Typescript: '{ translatedSentences: never[]; }' is not assignable to parameter of type 'never' 与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数 (() => 状态属性)' - React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)' “文件”类型的参数不能分配给“从不”类型的参数。 与 TypeScript 反应 - Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript 'string' 类型的参数不可分配给 typeScript 中的 'never' 类型的参数 - Argument of type 'string' is not assignable to parameter of type 'never' in typeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM