如何在 TypeScript 中创建只接受具有两个或更多元素的数组的类型?
needsTwoOrMore(["onlyOne"]) // should have error
needsTwoOrMore(["one", "two"]) // should be allowed
needsTwoOrMore(["one", "two", "three"]) // should also be allowed
2021 年更新: 较短的符号:
type arrMin1Str = [string, ...string[]]; // The minimum is 1 string.
OR
type arrMin2Strs = [string, string, ...string[]]; // The minimum is 2 strings.
OR
type arrMin3Strs = [string, string, string, ...string[]]; // The minimum is 3 strings.
OR …so on
这只是对 @KPD 和 @Steve Adams 答案的补充,因为所有指定的类型都是相同的。
自 TypeScript 3.0+(2018)以来,这应该是有效的语法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)