首页 > 解决方案 > 接口名称周围的方括号是什么意思?

问题描述

纯属偶然,我注意到以下代码并注意到 TypeScript 认为它是有效的:

interface AnInterface {
  foobar: string
} 
interface AnotherInterface {
  wtf: [AnInterface]
}

但究竟是哪种类型AnotherInterface['wtf']

标签: typescript

解决方案


这是一个元组类型,只有一个 type 元素AnInterface。基本上,它是一个单元素数组,其中唯一的元素是 type AnInterface


推荐阅读