使用 Typescript 的不可变 JS:"No overload matches this call" 创建 Map / OrderedMap 时
immutable JS with Typescript: "No overload matches this call" when creating Map / OrderedMap
我正在使用 Typescript 版本 3.8.3
和 immutable@4.0.0-rc.12
。
当我创建 OrderedMap
使用二维数组实例化它时,我从 Typescript 中得到错误。
let v: OrderedMap<number, string> = OrderedMap([ [ 3, '2' ] ])
似乎库的定义文件有误? (immutable-nonambient.d.ts(1421, 39)
)。这是它给我的错误:
No overload matches this call.
Overload 1 of 4, '(collection: Iterable<[string | number, string | number]>): OrderedMap<string | number, string | number>', gave the following error.
Argument of type '(string | number)[][]' is not assignable to parameter of type 'Iterable<[string | number, string | number]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<(string | number)[], any>' is not assignable to type 'IteratorResult<[string | number, string | number], any>'.
Type 'IteratorYieldResult<(string | number)[]>' is not assignable to type 'IteratorResult<[string | number, string | number], any>'.
Type 'IteratorYieldResult<(string | number)[]>' is not assignable to type 'IteratorYieldResult<[string | number, string | number]>'.
Type '(string | number)[]' is missing the following properties from type '[string | number, string | number]': 0, 1
Overload 2 of 4, '(obj: { [key: string]: string; }): OrderedMap<string, string>', gave the following error.
Type '(string | number)[]' is not assignable to type 'string'.ts(2769)
immutable-nonambient.d.ts(1421, 39): The expected type comes from this index signature.
我对 Map
也有同样的看法。如果我创建空的 Map
或 OrderedMap
并将其分配给变量,我可以在没有错误的情况下对数据执行操作,因此以这种方式进行初始化会遇到麻烦。
使用此签名实例化:OrderedMap({ 3: '2' })
没有编译错误。但显然我不能使用它,因为我希望我的键是数字,而使用数组表示法是实现它的唯一方法。
首先,@types/immutable
is no longer supported
This package has been deprecated
Author message:
This is a stub types definition for Facebook's Immutable (https://github.com/facebook/immutable-js). Facebook's Immutable provides its own type definitions, so you don't need @types/immutable installed!
删除它后,您应该会在编辑器中获得不错的类型提示。
工作示例:https://stackblitz.com/edit/typescript-vwetmv?file=index.ts
更新
在编译器选项中添加 "target": "es2015"
可消除错误。
查看文档:Using TypeScript with Immutable.js v4
Immutable.js type definitions embrace ES2015. While Immutable.js itself supports legacy browsers and environments, its type definitions require TypeScript's 2015 lib. Include either "target": "es2015"
or "lib": "es2015"
in your tsconfig.json
, or provide --target es2015
or --lib es2015
to the tsc
command.
我正在使用 Typescript 版本 3.8.3
和 immutable@4.0.0-rc.12
。
当我创建 OrderedMap
使用二维数组实例化它时,我从 Typescript 中得到错误。
let v: OrderedMap<number, string> = OrderedMap([ [ 3, '2' ] ])
似乎库的定义文件有误? (immutable-nonambient.d.ts(1421, 39)
)。这是它给我的错误:
No overload matches this call.
Overload 1 of 4, '(collection: Iterable<[string | number, string | number]>): OrderedMap<string | number, string | number>', gave the following error.
Argument of type '(string | number)[][]' is not assignable to parameter of type 'Iterable<[string | number, string | number]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<(string | number)[], any>' is not assignable to type 'IteratorResult<[string | number, string | number], any>'.
Type 'IteratorYieldResult<(string | number)[]>' is not assignable to type 'IteratorResult<[string | number, string | number], any>'.
Type 'IteratorYieldResult<(string | number)[]>' is not assignable to type 'IteratorYieldResult<[string | number, string | number]>'.
Type '(string | number)[]' is missing the following properties from type '[string | number, string | number]': 0, 1
Overload 2 of 4, '(obj: { [key: string]: string; }): OrderedMap<string, string>', gave the following error.
Type '(string | number)[]' is not assignable to type 'string'.ts(2769)
immutable-nonambient.d.ts(1421, 39): The expected type comes from this index signature.
我对 Map
也有同样的看法。如果我创建空的 Map
或 OrderedMap
并将其分配给变量,我可以在没有错误的情况下对数据执行操作,因此以这种方式进行初始化会遇到麻烦。
使用此签名实例化:OrderedMap({ 3: '2' })
没有编译错误。但显然我不能使用它,因为我希望我的键是数字,而使用数组表示法是实现它的唯一方法。
首先,@types/immutable
is no longer supported
This package has been deprecated
Author message:
This is a stub types definition for Facebook's Immutable (https://github.com/facebook/immutable-js). Facebook's Immutable provides its own type definitions, so you don't need @types/immutable installed!
删除它后,您应该会在编辑器中获得不错的类型提示。
工作示例:https://stackblitz.com/edit/typescript-vwetmv?file=index.ts
更新
在编译器选项中添加 "target": "es2015"
可消除错误。
查看文档:Using TypeScript with Immutable.js v4
Immutable.js type definitions embrace ES2015. While Immutable.js itself supports legacy browsers and environments, its type definitions require TypeScript's 2015 lib. Include either
"target": "es2015"
or"lib": "es2015"
in yourtsconfig.json
, or provide--target es2015
or--lib es2015
to thetsc
command.