首页 > 解决方案 > 在 TypeScript 中使用嵌套类型?

问题描述

我想将类型添加到应该接收items字符串数组的函数中:

type Inputs = {
  foo: string
  items: string[];
};


const myFunction = (items: string[]) => {
    //
}

上面的代码有效,但是有没有办法从 Inputs 类型中获取类型?所以像:

const myFunction = (items: Inputs.items) => {
    //
}

标签: typescript

解决方案


当然可以,但只需使用括号表示法:

type Inputs = {
  foo: string
  items: string[];
};


const myFunction = (items: Inputs['items']) => {
  //
}

推荐阅读