我正在使用Typescript 2.9.2,React 16.7.21和React-redux 7.0.1。
使用immutableJS 3.8.2,此代码可以很好地工作:
import { Map } from "immutable";
export default function createState() {
return Map<string, any>({
state: new AppState()
});
}
export type State = Map<string, any>;
使用immutableJS 4.0.0时,它因该错误而中断:
Argument of type '{ state: AppState }' is not assignable to parameter of type 'Iterable<[string, any]>'. Object literal may only specify known properties, and 'state' does not exist in type 'Iterable<[string, any]>'.
要使用immutableJS,经过大量测试,我发现它必须在返回Map中省略“字符串”,因此此代码不会出现错误:
import { Map } from "immutable";
export default function createState() {
return Map<any>({
state: new AppState()
});
}
export type State = Map<string, any>;
但是编译它会产生此编译错误:
Failed to compile.
Type error: Argument of type '{ state: AppState; }' is not assignable to parameter of type 'Iterable<[string, any]>'.
Object literal may only specify known properties, and 'state' does not exist in type 'Iterable<[string, any]>'. TS2345
68 | export default function createState() {
69 | return Map<string, any>({
> 70 | state: new AppState()
| ^
71 | });
72 | }
73 |
我不知道为什么...任何人都可以向我解释它?
我认为答案就在这里,但我没有得到:https://immutable-js.github.io/immutable-js/docs/#/Map
Map()
Creates a new Immutable Map.
Map<K, V>(collection: Iterable<[K, V]>): Map<K, V>
Map<V>(obj: {[key: string]: V}): Map<string, V>
Map<K, V>(): Map<K, V>
Map(): Map<any, any>