用Typescript中的数组初始化映射:新映射([[k1,v1],[k2,v2]]

用Typescript中的数组初始化映射:新映射([[k1,v1],[k2,v2]],typescript,Typescript,这两个初始化不应该是等价的 这很好用: private screensMap: Map<string, ComponentType<any>>; public constructor() { this.screensMap = new Map() .set(BootstrapLaunch.name, BootstrapLaunch) .set(BootstrapWelcome.name, BootstrapWelcome); }

这两个初始化不应该是等价的

这很好用:

private screensMap: Map<string, ComponentType<any>>;

public constructor() {
    this.screensMap = new Map()
        .set(BootstrapLaunch.name, BootstrapLaunch)
        .set(BootstrapWelcome.name, BootstrapWelcome);
}
私有屏幕Map:Map;
公共构造函数(){
this.screensMap=newmap()
.set(BootstrapLaunch.name,BootstrapLaunch)
.set(BootstrapWelcome.name,BootstrapWelcome);
}
但这是失败的:

private screensMap: Map<string, ComponentType<any>>;

public constructor() {
    this.screensMap = new Map([
        [BootstrapLaunch.name, BootstrapLaunch],
        [BootstrapWelcome.name, BootstrapWelcome],
    ]);
}
私有屏幕Map:Map;
公共构造函数(){
this.screensMap=新地图([
[BootstrapLaunch.name,BootstrapLaunch],
[BootstrapWelcome.name,BootstrapWelcome],
]);
}
下一个错误是:

error TS2345: Argument of type '([string, typeof BootstrapLaunch] | [string, typeof BootstrapWelcome])[]' is not assignable to parameter of type 'ReadonlyArray<[string, typeof BootstrapLaunch]>'.
  Type '[string, typeof BootstrapLaunch] | [string, typeof BootstrapWelcome]' is not assignable to type '[string, typeof BootstrapLaunch]'.
    Type '[string, typeof BootstrapWelcome]' is not assignable to type '[string, typeof BootstrapLaunch]'.
      Type 'typeof BootstrapWelcome' is not assignable to type 'typeof BootstrapLaunch'.
        Type 'BootstrapWelcome' is not assignable to type 'BootstrapLaunch'.
          Types of property 'componentDidMount' are incompatible.
            Type '(() => void) | undefined' is not assignable to type '() => void'.
              Type 'undefined' is not assignable to type '() => void'.
错误TS2345:类型为“([string,typeof BootstrapLaunch]|[string,typeof bootstrapHelpCome])[]”的参数不能分配给类型为“ReadonlyArray”的参数。
类型“[string,typeof BootstrapLaunch]|[string,typeof BootstrapWelcome]”不能分配给类型“[string,typeof BootstrapLaunch]”。
类型“[string,typeof BootstrapWelcome]”不能分配给类型“[string,typeof BootstrapLaunch]”。
类型“typeof BootstrapHelpCome”不可分配给类型“typeof BootstrapLaunch”。
类型“BootstrapWelcome”不可分配给类型“BootstrapLaunch”。
属性“componentDidMount”的类型不兼容。
类型“(()=>void)|未定义”不可分配给类型“()=>void”。
类型“undefined”不可分配给类型“()=>void”。

这是一个Typescript错误还是我做错了什么?

Typescript试图推断您的值的类型,但由于您的组件在实现
componentDidMount
方面不同,它试图警告您可能存在的编程错误

由于所需的值类型比提供的组件类型更广泛,因此可以使用类型参数告诉TypeScript您的意图:

public constructor() {
    this.screensMap = new Map<string, ComponentType<any>>([
        [BootstrapLaunch.name, BootstrapLaunch],
        [BootstrapWelcome.name, BootstrapWelcome],
    ]);
}
public构造函数(){
this.screensMap=新地图([
[BootstrapLaunch.name,BootstrapLaunch],
[BootstrapWelcome.name,BootstrapWelcome],
]);
}

在我看来像是一个类型脚本错误,无法为
typeof BootstrapWelcome
typeof BootstrapLaunch
找到一个通用的超类型。尝试显式地将它们转换为
ComponentType
。使用显式转换为
ComponentType
会起作用。