首页 > 解决方案 > 类型 'never[]' 不能分配给类型 '...'

问题描述

类型 'never[]' 不能分配给类型 '...'

由于这个随机错误,我无法初始化我班级的属性项。我想扩展泛型类类型以Array<string>确保该类型始终是字符串数组?

class MyClass<TItems extends Array<string>> {
    constructor() {
        this.items = [];
    }

    public items: TItems;
}

给我错误:

类型“字符串”不可分配给类型“TItems”。“string”可分配给“TItems”类型的约束,但“TItems”可以用约束“string”的不同子类型实例化。ts(2322)

标签: typescript

解决方案


问题

您可能熟悉可以将子类型分配给超类型的事实,但反之亦然。因此,在以下代码和内联解释中 -

class A extends Array<string> {
  public myProp!: string
}

// it is ok to assign a subtype to its supertype
// because subtype has atleast all those props/methods
// that a supertype has
declare const a1: A
const array1: Array<string> = a1

// it is an error to assign supertype to one of its subtype
// (until they are structurally same)
// because the supertype (array2) may have some missing props/methods
// (myProp in this case) that its subtype has.
declare const array2: Array<string>
const a2: A = array2

操场

在您的代码中,TItems是 的子类型Array<string>,而类型[]never[]

如果您使用 对其进行了类型转换,则无法[] as Array<string>将超类型 ( ) 分配给子类型。操场Array<string>TItems

如果您使用 对其进行[] as TItems了类型转换,则出于同样的原因,类型转换本身就是错误的。操场

解决方案

可以通过类型转换为错误来消除该错误-

class MyClass<TItems extends Array<string>> {
    public items: TItems;

    constructor() {
        this.items = [] as unknown as TItems;
    }
}

操场

但这可能会导致运行时错误,因为它不是“安全”的类型转换。

为了避免运行时错误,正确的方法是使用返回items的类TItems或函数的构造函数来初始化 prop,TItems而不是= []. 这将消除类型错误并确保不会出现运行时错误。两种方式都得到证明——

// if a passed TItems is supposed to class
// we pass that constructor of the class 
// constructor of `MyClass`
class MyClass<TItems extends Array<string>> {
    public items: TItems;

    constructor(ctor: new () => TItems) {
        this.items = new ctor();
    }
}

class MyArray extends Array<string> {
  private myProp!: string
}

const myClassVar = new MyClass(MyArray)

操场

// if a passed TItems is supposed to be just a type
// we pass a function that will create that object of `TItems`
class MyClass<TItems extends Array<string>> {
    public items: TItems;

    constructor(fn: () => TItems) {
        this.items = fn();
    }
}

declare function createObject(): Array<string> & { myProp: string }

const myClassVar = new MyClass(createObject)

操场


推荐阅读