区分类的联合

JAn*_*Man 6 union discriminated-union typescript

使用 Typescript 3.4,我尝试根据每个子类上的单个只读字段(鉴别器)来区分 Typescript 类的联合;其本身似乎很简单,但我似乎无法解决它。

下面是一个 Playground 的链接,其中 createFruit 方法应该能够采用通用参数来通过鉴别器过滤下一个属性。任何见解将不胜感激;然而,它似乎允许评估从不。

abstract class Fruit {
    abstract readonly fruitType: string;
}

class Banana extends Fruit {
    fruitType = 'banana';
    length = 2;
    color = 'yellow';
}

class Pear extends Fruit {
    fruitType = 'pear';
    roundness = 'very round';
}

class Apple extends Fruit {
    fruitType = 'apple';
    fallOfMan = true;
    hasWorms = true;
}

type KnownFruits = Banana | Pear | Apple;

type FruitTypes = KnownFruits['fruitType'];

type FruitDiscriminator<T extends FruitTypes> = Extract<KnownFruits, { fruitType: T }>;

let createFruit = <T extends FruitTypes>(fruitType: T, props: FruitDiscriminator<T>) => { }

createFruit('pear', {} ) // Argument of type '{}' is not assignable to parameter of type 'never'.
Run Code Online (Sandbox Code Playgroud)

TS游乐场

Tit*_*mir 7

问题在于fruitType派生类中的类型string不是与字符串文字关联的字符串文字类型。默认情况下,Typescript 会在初始化字段时将文字类型扩展为基本类型。要解决这个问题,您可以使用as const断言,或创建 w 字段readonly(或手动指定字符串文字类型,但这会重复字符串)

readonly解决方案最有意义,因为类型不应更改:

abstract class Fruit {
    abstract readonly fruitType: string;
}

class Banana extends Fruit {
    readonly fruitType = 'banana';
    length = 2;
    color = 'yellow';
}

class Pear extends Fruit {
    readonly fruitType = 'pear';
    roundness = 'very round';
}

class Apple extends Fruit {
    readonly fruitType = 'apple';
    fallOfMan = true;
    hasWorms = true;
}

type KnownFruits = Banana | Pear | Apple;

type FruitTypes = KnownFruits['fruitType'];

type FruitDiscriminator<T extends FruitTypes> = Extract<KnownFruits, { fruitType: T }>;

let createFruit = <T extends FruitTypes>(fruitType: T, props: FruitDiscriminator<T>) => { }

// ok
createFruit('pear', {
    fruitType: 'pear',
    roundness: ""
})
Run Code Online (Sandbox Code Playgroud)