gpt4 book ai didi

javascript - 在类型定义中使用另一种类型的属性?

转载 作者:行者123 更新时间:2023-12-01 01:43:53 25 4
gpt4 key购买 nike

我正在阅读this article它有以下类型定义:

 export type CartItem = {
productId: Product['id'];
quantity: number;
total: number;
};

我假设这意味着 productId 属性必须具有与分配给 product['id'] 的类型相同的类型?仅供引用,其定义如下:

export type Product = {
id: ID;
title: string;
description: string;
price: number;
};

最佳答案

是的,Product['id']indexed access type (也称为查找类型),获取 Product 类型的字段 id 的类型,即 ID

关于javascript - 在类型定义中使用另一种类型的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52124090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com