在 TypeScript 中递归展开数组类型
我需要创建一个可以获取非常“动态”参数的函数。它应该能够接受多种类型的数组
class NdArray<T> {
}
// need to be able to get
f(number[]) // -> NdArray<number>
f(number[][]) // -> NdArray<number>
f(number[][][]) // -> NdArray<number>
//and so on...
f(string[]) // -> NdArray<string>
f(string[][]) // -> NdArray<string>
f(string[][][]) // -> NdArray<string>
// and generally
f(object[][][]...) // -> NdArray<object>
I need to create a function that can get a very "dynamic" parameter. it should be a ble to accept many types of arrays
class NdArray<T> {
}
// need to be able to get
f(number[]) // -> NdArray<number>
f(number[][]) // -> NdArray<number>
f(number[][][]) // -> NdArray<number>
//and so on...
f(string[]) // -> NdArray<string>
f(string[][]) // -> NdArray<string>
f(string[][][]) // -> NdArray<string>
// and generally
f(object[][][]...) // -> NdArray<object>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
递归地解开数组:
如果
A
是一个数组,我们就解开其元素的类型。否则,这只是我们不需要打开的其他东西。您的函数
f
可能类似于:游乐场
Recursively unwrap the array:
If
A
is an array, we unwrap the type of its elements. Otherwise it's just something else we don't need to unwrap.Your function
f
here could be something like:Playground