在类型定义中使用其他类型的属性?

时间:2018-09-01 01:07:03

标签: javascript typescript

我正在阅读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;
};

1 个答案:

答案 0 :(得分:2)

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

相关问题