如何在 TypeScript 中使用嵌套对象? 属性 在类型 'object' 上不存在

How to use nested objects in TypeScript? Property does not exist on type 'object'

我有一个 React/GraphQL 运行良好的小型应用程序,我正在尝试向其中添加 TypeScript,因为我是 TypeScript 的新手并正在尝试学习它。我有一个 GraphQL API 端点,它正在 returning 产品和这些产品的信息。使用 React,我将产品存储在一个状态中,然后使用 JSX 渲染每个产品。

我创建了一个类型 Product,其中包含我期望 API 到 return 的信息,但它也是 returning 嵌套对象,我尝试的所有操作都会触发TypeScript 错误。这是 API 是什么的简化示例 returning:

{
    "data": {
        "products": [
            {
                "productName": "Soda",
                "code": "sod",
                "prices": {
                    "nationalPrices": {
                        "localPrices": [
                            {
                                "pricePerUnit": "1.99"
                            }
                        ],
                        "regionalPrices": [
                            {
                                "pricePerUnit": "2.49"
                            }
                        ]
                    },
                    "vendorPrices": {
                        "localPrices": [
                            {
                                "pricePerUnit": "1.49"
                            }
                        ]
                    }
                }
            },
            // more products... 
        ]
    }
}

这是我目前的解决方案。 codeproductNameprices 工作正常,但 nationalPrices 触发了 TypeScript 错误 property nationalPrices does not exist on type 'object'。我该怎么做才能修复此错误?

type nationalPricesObject = {
    localPrices: object[];
}

type Product = {
    code: string;
    productName: string;
    prices: object;
    nationalPrices: nationalPricesObject;
}


function ProductList() {
    const [products, setProducts] = useState < Product[] > ([]);
    const { loading, data, error } = useQuery(LOAD_PRODUCTS);

    useEffect(() => {
        if (data) {
            setProducts(data.products);
        }
    }, [data]);

    return (
        <>
            <div>
                {products.map(p =>
                (
                    <div>
                        <ProductCard
                            productName={p.displayName}
                            code={p.code}
                            // simplified code below for sake of clearity 
                            pricePerUnit={p.prices.nationalPrices.localPrices[0]['pricePerUnit']}
                        />
                    </div>
                ))}
            </div>
        </>
    )
}

产品不包含 nationalPrices 属性。 Product.prices 确实如此。我们可以设置一个价格类型,其中包含 Product.prices 中预期的内容,并使用它来定义 Product 对象的价格 属性。

type Prices = {
    nationalPrices: nationalPricesObject;
    vendorPrices: someOtherPricesObject;
}

type Product = {
    code: string;
    productName: string;
    prices: Prices;
}

object 的用法在打字稿中很棘手,通常不推荐使用。你这里有两个问题:

  1. object 是一个不明确的类型,所以你不能使用 prices.nationalPrices 因为 nationalPrices 在类型 object
  2. 上不存在
  3. 修复此问题后,您还会遇到类型为 objectlocalPrices[0] 没有 pricePerUnit
  4. 的错误

要解决这些问题,请输入更具体的内容:

type nationalPricesObject = {
    localPrices: { pricePerUnit: number; }[]; // assuming this is number?
}

type Product = {
    code: string;
    productName: string;
    prices: nationalPricesObject;
}

最后,按照惯例,typescript 中的类型应该是 PascalCase,接口应该是首选。因此,我会将您的代码更改为:

interface Price {
  pricePerUnit: number;
}

interface NationalPrices {
    localPrices: Price[];
}

interface Product {
    code: string;
    productName: string;
    prices: NationalPrices;
}