在TypeScript中使用嵌套类型吗?

问题描述 投票:0回答:1

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

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


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

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

const myFunction = (items: Inputs.items) => {
    //
}
typescript
1个回答
1
投票

当然,但是请使用方括号表示法:

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


const myFunction = (items: Inputs['items']) => {
  //
}
© www.soinside.com 2019 - 2024. All rights reserved.