假设我有function foo(args) {...},其中args是一个二元组数组,这样元组中的条目是相同类型的(即[T,T]),但是跨元组的条目可能任意变化(即[[T,T], [U,U],[V,V]])。例如:
foo([ [1, 3], ["hello", "world"], [true, true], [2, 7] ]) // no error
我应该如何输入 foo 的 args 参数,以便元组中的不匹配类型引发编译时类型错误?例如:
foo([ [1, 3], ["hello", 5], // type error here [true, true], [2, 7n] // type error here ])
如果无法内联显示类型错误,则使整个函数调用错误也是可以接受的。
附录:是否可以使用 [SomeType 类型的 2 元组(即第二个条目的类型应与第一个),但 T 仍然可以在元组之间变化 [[SomeType?
foo([
[{value: 1}, 3],
[{value: "hello"}, 5], // type error here
[{value: true}, true],
[{value: 2}, 7n] // type error here
])
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号