Reactjs 类型为'的参数;未知';不可分配给类型为';{}';

Reactjs 类型为'的参数;未知';不可分配给类型为';{}';,reactjs,typescript,Reactjs,Typescript,这是我的密码 const Res = await fetch(`https://foo0022.firebaseio.com/.json`); const ResObj = await Res.json(); if (!Res.ok || !ResObj) { throw new Error("Page Not Found 404"); } const ResArr = await Object.value

这是我的密码

 const Res = await fetch(`https://foo0022.firebaseio.com/.json`);
        const ResObj = await Res.json();
        if (!Res.ok || !ResObj) { 
          throw new Error("Page Not Found 404");
        } 
        const ResArr = await Object.values(ResObj)
            .map(v => Object.values(v).flat())//error
            .flat()
            .filter(({ title }) => title.includes(Search))

在行中的行中,我得到了这个错误“.map(v=>Object.values(v).flat())”我得到了这个类型为“unknown”的错误参数不能分配给类型为“{}”的参数。这个问题如何解决?

这里的问题是,您需要帮助TypeScript理解您正在处理的对象的类型。
fetch
API无法预先知道返回对象的形状,因此您必须定义它并断言结果符合它

看看目前的情况,我建议如下:

interface ResObj {
  Mens: {
    Hat: Clothing[];
    Jacket: Clothing[];
    Pants: Clothing[];
    Shoes: Clothing[];
    Suit: Clothing[];
  };
  New: Clothing[];
}
interface Clothing {
  agility: boolean[];
  alt: string;
  color: string[][];
  id: string;
  location?: string; // fix this
  Location?: string; // fix this
  material: string;
  price: string[][];
  prodState: string;
  saiz: string[][];
  shipping: string;
  sold: string;
  src: string[][];
  title: string;
  to: string;
}
当然,这是否准确取决于某种API文档。假设这是正确的,您可以进一步:

  const Res = await fetch(`https://foo0022.firebaseio.com/.json`);
  const ResObj: ResObj | undefined = await Res.json();
  if (!Res.ok || !ResObj) {
    throw new Error("Page Not Found 404");
  }
现在,
ResObj
将被称为类型
ResObj
,您可以开始操作它。其中一个问题是,标准库的打字方式并没有反映出你在用它们做什么。我们可以为他们定制一些打字。。。但在本例中,我将使用类型匹配的新函数包装它们:

  // return an array of all object values...
  // if the object is already an array, the output is the same type.
  // otherwise it's the union of all the known property types
  function vals<T extends object>(
    arr: T
  ): Array<T extends Array<infer U> ? U : T[keyof T]> {
    return Object.values(arr); // need es2017 lib for this
  }

  // Flatten an array by one level... 
  function flat<T>(
    arr: Array<T>
  ): Array<Extract<T, any[]>[number] | Exclude<T, any[]>> {
    return arr.flat(); // need esnext lib for this
  }
并且没有错误,并且编译器理解
ResArr
是一个
衣服
对象数组

好吧,希望这会有帮助;祝你好运

问题
Res.json()。当启用
stricnullchecks
时,TypeScript将不允许我们将
unknown
类型的值分配给
{}
类型的参数

这一解释也与评论一致

const func = async () => {

    const Res = await fetch(`https://foo0022.firebaseio.com/.json`);

    /**
     * The ResObj is that `Res.json()` returns is of type `any`.
     */
    const ResObj = await Res.json();

    if (!Res.ok || !ResObj) {
        throw new Error("Page Not Found 404");
    }

    /**
     * When we pass Object.values a type of `any`, 
     * it produces an array of type `unknown[]`.
     */
    const unknownArray = Object.values(ResObj);

    /**
     * `Object.values` has two signatures: 
     * 
     * * `values(o: {}): any[];`
     * * `values<T>(o: { [s: string]: T } |  ArrayLike<T>): T[];`
     * 
    * When `strictNullCheck` is `true`, we cannot assign `unknown` to `{}`.
    */
    const ResArr = unknownArray.map(unknownItem => Object.values(unknownItem));
};

好吧,你还没有在这里定义任何类型,我首先要说:)天哪,那函数类型。。。真恶心
const func = async () => {

    const Res = await fetch(`https://foo0022.firebaseio.com/.json`);

    /**
     * The ResObj is that `Res.json()` returns is of type `any`.
     */
    const ResObj = await Res.json();

    if (!Res.ok || !ResObj) {
        throw new Error("Page Not Found 404");
    }

    /**
     * When we pass Object.values a type of `any`, 
     * it produces an array of type `unknown[]`.
     */
    const unknownArray = Object.values(ResObj);

    /**
     * `Object.values` has two signatures: 
     * 
     * * `values(o: {}): any[];`
     * * `values<T>(o: { [s: string]: T } |  ArrayLike<T>): T[];`
     * 
    * When `strictNullCheck` is `true`, we cannot assign `unknown` to `{}`.
    */
    const ResArr = unknownArray.map(unknownItem => Object.values(unknownItem));
};
type MyKnownType = {
    prop1: string;
    prop2: number;
    prop3: boolean;
};

const ResObj: MyKnownType = await Res.json();