简体   繁体   English

[type] 在打字稿/流程中是什么意思?

[英]What does the [type] mean in typescript/flow?

I've recently stumbled upon an old code of mine, I noticed I had incorrect typing there.我最近偶然发现了我的一个旧代码,我注意到我在那里打字不正确。

interface OptionType {
   label: string,
   value: OptionValueType
}

interface Props {
  options: [OptionType] // < this is wrong
}

Now obviously, this should have been OptionType[] or Array<OptionType> , but curiously enough, the code here, at least according to both linter ( ) and compiler is valid syntax.现在很明显,这应该是OptionType[]Array<OptionType> ,但奇怪的是,这里的代码,至少根据 ( ) 和编译器是有效的语法。

So, what exactly does [OptionType] represent?那么, [OptionType]到底代表什么?

My best guess would be an index/element of an array, akin to destructuring, but that feels odd as there's no original array reference in the syntax我最好的猜测是数组的索引/元素,类似于解构,但这感觉很奇怪,因为语法中没有原始数组引用

It's a tuple.这是一个元组。 It's syntax allows you to specify an array with exactly n elements, and give a type for each:它的语法允许您指定一个恰好包含n 个元素的数组,并为每个元素指定一个类型:

const myTuple: [string, number] = ['foo', 5];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM