是否可以使用“动态字段名称”创建类型?

时间:2020-09-23 14:58:30

标签: typescript

想象一下我有以下两种类型:


type Product = {
  id: number
  sku: string
  name: string
}

type CsvProps = {
  encoding: string
  delimiter: string
  quotechar: string
  aditionalMapFields: {[k: string]: string}
}

我从请求中获得了CsvProps,并创建了一个新的对象,如下所示:

  const csvProps: CsvProps = axios..... 
  const product: Product = {id: 1, sku: 'a', name: 'b'}
  const productWithAditionalFields = {...product, ...csvProps.aditionalMapFields }

有没有一种方法可以为此productWithAditionalFields对象创建类型? 香港专业教育学院尝试过愚蠢的方法,但它不起作用:


type ProductWithAditionalProps = Product & {[k: keyof CsvProps.aditionalMapFields]: string}

0 个答案:

没有答案