【发布时间】:2021-03-26 18:30:08
【问题描述】:
我有一个糟糕的数据结构,导致 TypeScript 崩溃。
let foo = {
'baz':[
[[x1, x2, x3], (a: any, b: number, c: number) => 5],
[[x4], (a: number) => 7]],
'bar':[
[[], () => 0]]
}
x1, x2... 都是(s: string) => number | string 函数。我正在努力明确定义 foo's 类型。在 Java 风格的术语中,它会是
Map<String, List<Tuple<List<Function<String,Any>>, Function<Any...,Integer>>>>
列表的列表,其中内部列表具有函数列表和函数。顺便说一句,函数列表中的元素数量将始终等于匿名函数中的参数数量。 TypeScript 编译器推荐:
Record<string, (any[] | ((...args: any) => number))[][]>
但是,当我尝试调用 foo['baz'][0][0][2](myString),调用 x3(myString) 时,编译器会大喊
TS7053: Element implicitly has an 'any' type because expression of type 'number' can't be used
to index type 'any[] | ((...args: any) => number)'. No index signature with a parameter of
type 'number' was found on type 'any[] | ((...args: any) => number)'.
【问题讨论】:
标签: typescript types type-hinting