[英]TypeScript: how to declare array of fixed size for type checking at Compile Time
Update : These checks are meant for compile time , not at runtime . 更新 :这些检查用于编译时 ,而不是在运行时 。 In my example, the failed cases are all caught at compile time, and I'm expecting similar behaviour for the other should-fail cases.
在我的例子中,失败的案例都是在编译时捕获的,我期待其他应该失败的情况下的类似行为。
Suppose I'm writing a table-like class where I want all members of the class to be arrays of the same length, something like: 假设我正在编写一个类似于表的类,我希望该类的所有成员都是相同长度的数组,如:
class MyClass {
tableHead: string[3]; // expect to be a 3 element array of strings
tableCells: number[3]; // expect to be a 3 element array of numbers
}
The closest solution I've found so far is: 到目前为止我找到的最接近的解决方案是:
class MyClass {
tableHead: [string, string, string];
tableCells: [number, number, number];
}
let bar = new MyClass();
bar.tableHead = ['a', 'b', 'c']; // pass
bar.tableHead = ['a', 'b']; // fail
bar.tableHead = ['a', 'b', 1]; // fail
// BUT these also pass, which are expected to fail at compile time
bar.tableHead = ['a', 'b', 'c', 'd', 'e']; // pass
bar.push('d'); // pass
bar.push('e'); // pass
Any better ideas? 有更好的想法吗?
Update 2: From version 3.4, what the OP asked for is now fully possible with a succinct syntax ( Playground link ): 更新2:从版本3.4开始,OP所要求的内容现在完全可以使用简洁的语法( Playground链接 ):
class MyClass {
tableHead: readonly [string, string, string]
tableCells: readonly [number, number, number]
}
Update 1: From version 2.7, TypeScript can now distinguish between lists of different sizes . 更新1:从版本2.7开始,TypeScript现在可以区分不同大小的列表 。
I don't think it's possible to type-check the length of a tuple. 我不认为可以输入 - 检查元组的长度。 Here 's the opinion of TypeScript's author on this subject.
以下是TypeScript作者对此主题的看法。
I'd argue that what you're asking for is not necessary. 我认为你所要求的并不是必需的。 Suppose you define this type
假设您定义了此类型
type StringTriplet = [string, string, string]
and define a variable of that type: 并定义该类型的变量:
const a: StringTriplet = ['a', 'b', 'c']
You can't get more variables out of that triplet eg 你不能从那个三元组中获得更多的变量,例如
const [one, two, three, four] = a;
will give an error whereas this doesn't as expected: 会出错,而这不符合预期:
const [one, two, three] = a;
The only situation where I think the lack of ability to constrain the length becomes a problem is eg when you map
over the triplet 我认为缺乏约束长度的能力成为问题的唯一情况是例如当你
map
三元组时
const result = a.map(/* some pure function */)
and expect that result
have 3 elements when in fact it can have more than 3. However, in this case, you are treating a
as a collection instead of a tuple anyway so that's not a correct use case for the tuple syntax. 并且期望
result
有3个元素,实际上它可以有3个以上。但是,在这种情况下,你无论如何都将a
视为集合而不是元组,因此这不是元组语法的正确用例。
Here is a simple example of a class to control the length of its internal array. 下面是一个控制其内部数组长度的类的简单示例。 It isn't fool-proof (when getting/setting you may want to consider whether you are shallow/deep cloning etc:
这不是万无一失的(在获取/设置时你可能想要考虑你是浅层/深层克隆等:
https://jsfiddle.net/904d9jhc/ https://jsfiddle.net/904d9jhc/
class ControlledArray {
constructor(num) {
this.a = Array(num).fill(0); // Creates new array and fills it with zeros
}
set(arr) {
if (!(arr instanceof Array) || arr.length != this.a.length) {
return false;
}
this.a = arr.slice();
return true;
}
get() {
return this.a.slice();
}
}
$( document ).ready(function($) {
var m = new ControlledArray(3);
alert(m.set('vera')); // fail
alert(m.set(['vera', 'chuck', 'dave'])); // pass
alert(m.get()); // gets copy of controlled array
});
From Typescript: Can I define an n-length tuple type? 来自Typescript:我可以定义一个n长度的元组类型吗? , programmatically, with dynamic length:
,以编程方式,具有动态长度:
type Tuple<TItem, TLength extends number> = [TItem, ...TItem[]] & { length: TLength };
type Tuple9<T> = Tuple<T, 9>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.