Typescript &引用;没有与此调用匹配的重载“;从数组构造映射时
Typescript &引用;没有与此调用匹配的重载“;从数组构造映射时,typescript,Typescript,newmap([[1,2,3]]].Map(e=>e[1])是完全有效的javascript。但是typescript给出了以下错误: No overload matches this call. Overload 1 of 3, '(iterable: Iterable<readonly [unknown, unknown]>): Map<unknown, unknown>', gave the following error. Argument of ty
newmap([[1,2,3]]].Map(e=>e[1])
是完全有效的javascript。但是typescript给出了以下错误:
No overload matches this call.
Overload 1 of 3, '(iterable: Iterable<readonly [unknown, unknown]>): Map<unknown, unknown>', gave the following error.
Argument of type '(number | number[])[]' is not assignable to parameter of type 'Iterable<readonly [unknown, unknown]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<number | number[], any>' is not assignable to type 'IteratorResult<readonly [unknown, unknown], any>'.
Type 'IteratorYieldResult<number | number[]>' is not assignable to type 'IteratorResult<readonly [unknown, unknown], any>'.
Type 'IteratorYieldResult<number | number[]>' is not assignable to type 'IteratorYieldResult<readonly [unknown, unknown]>'.
Type 'number | number[]' is not assignable to type 'readonly [unknown, unknown]'.
Type 'number' is not assignable to type 'readonly [unknown, unknown]'.
Overload 2 of 3, '(entries?: readonly (readonly [unknown, unknown])[] | null | undefined): Map<unknown, unknown>', gave the following error.
Argument of type '(number | number[])[]' is not assignable to parameter of type 'readonly (readonly [unknown, unknown])[]'.
Type 'number | number[]' is not assignable to type 'readonly [unknown, unknown]'.
Type 'number' is not assignable to type 'readonly [unknown, unknown]'.
没有与此调用匹配的重载。
重载1/3'(iterable:iterable):Map',产生以下错误。
类型为“(number | number[])[]”的参数不能分配给类型为“Iterable”的参数。
“[Symbol.iterator]().next(…)”返回的类型在这些类型之间不兼容。
类型“IteratorResult”不可分配给类型“IteratorResult”。
类型“IteratorYieldResult”不可分配给类型“IteratorResult”。
类型“IteratorYieldResult”不可分配给类型“IteratorYieldResult”。
类型“number | number[]”不能分配给类型“readonly[unknown,unknown]”。
类型“number”不可分配给类型“readonly[unknown,unknown]”。
重载2/3’(条目?:readonly(readonly[未知,未知])[]| null |未定义):Map’给出了以下错误。
类型为“(number | number[])[]”的参数不能分配给类型为“readonly(readonly[unknown,unknown])[]”的参数。
类型“number | number[]”不能分配给类型“readonly[unknown,unknown]”。
类型“number”不可分配给类型“readonly[unknown,unknown]”。
编译器似乎正在将Map
的签名与[[1,2,3]]
匹配,而不是将转换后的[2,3]
匹配
如果Map
仅采用只读数组,则进行后续操作:
newmap(Object.freeze([[1,2,3]]]].Map(e=>e[1]);
即使Object.freeze
返回readonly
数组也不工作TS编译器在理论上应该能够推导出所有三种形式的等价性。让我们深入研究
MapConstructor
接口映射构造函数{
new():映射和下一个示例:
constinfer=(arg:readonly[K,V])=>arg
常数x=推断([1,2])
正如您所看到的,因为您有文字数组,TS能够计算出您正好有两个值:key和value
但是,如果使用引用作为参数(请再次参阅多余属性检查):
常量推断=(arg:readonly[K,V])=>arg
常量元组=[1,2]//number[]
常量x=推断(元组)
TS会抱怨,baceusetuple
被推断为number[]
,TS无法计算数组中有多少元素。它甚至可以是空数组。这就是TS不允许您在此处引用可变数组的原因
为什么新映射(Object.freeze([[1[2,3]]]].Map(e=>e))不起作用
这两者之间有很大的区别:
Object.freeze([[1,2,3]]].map(e=>e[1]))
及
([[1,2,3]]]as const.map(e=>e[1])
在第一种情况下,您可以在map
谓词中变异数组,但无法变异结果
在第二种情况下,您无法在映射中变异数组
请查看他们的类型签名:
type First=readonly(number | number[])[]
键入Second=(只读[2,3])[]
允许使用x:First=[1]//但参数无效
设y:Second=[1]//是不允许的,因为我们应该有一个kay和value。而不仅仅是key
谢谢。两个问题:1.为什么新地图([[2,3]]);
是正确的?2.为什么新地图(Object.freeze([[1,2,3]]]].Map(e=>e[1]));
不起作用,因为对象。freeze
返回readonly
数组?TS编译器理论上应该能够推导出所有三种形式的等价性。@idlewords很好,我做了一个更新