首页 > 解决方案 > TypeScript 推断从不输入,但需要赋值

问题描述

在我的项目中,我有一个充当文件通用类型的类。根据我们正在处理的文件类型,它应该公开其他属性。

never我尝试使用默认为“隐藏”属性的条件类型来实现这一点。但是,当我尝试使用该类时,类型检查器会抱怨我缺少被推断为 type 的属性never。当然,我不能分配它,所以我留下了一个无法创建的对象。

错误发生在这段代码的底部:

// just for convenience
type MP4OptionsT = {
    codec?: 'h264',
    profile: 'baseline' | 'main' | 'high',
    bitrate: number,
};

// this is the class in question
class MediaFile<Format extends 'mp4' | 'png'> {
    public path: string;
    public format: Format extends 'mp4' ? 'mp4' : Format extends 'png' ? 'png' : never;    // once the generic type argument is set, this can only be a specific string literal

    // this should not have to be assigned if generic type argument is 'png'
    public mp4Options: Format extends 'mp4' ? MP4OptionsT : never;

    constructor(opts: {
        path: string,
        format: Format extends 'mp4' ? 'mp4' : Format extends 'png' ? 'png' : never;
        // this should not have to be assigned if generic type argument is 'png' - however it demands to be assigned
        mp4Options: Format extends 'mp4' ? MP4OptionsT : never,
    }) {
        this.path = opts.path;
        this.format = opts.format;
        this.mp4Options = opts.mp4Options;
    }
}

// this is OK
const mp4File = new MediaFile<'mp4'>({
    path: '/some/file/somewhere.mp4',
    format: 'mp4',
    mp4Options: {
        profile: 'high',
        bitrate: 1000,
    }
});

// the type checker complains about this: "Property mp4Otions is missing in type {...}".
// if I explicitly include mp4Options, the type checker notes that "Type any is not assignable to Type never" - which makes sense, but precludes this class from ever being instantiated.
const pngFile = new MediaFile<'png'>({
    path: '/some/file/somewhere.png',
    format: 'png',    // since there is exactly one option for this, it would be nice if it were implicitly set...
});

根据我对本页条件类型部分http://www.typescriptlang.org/docs/handbook/advanced-types.html的理解,似乎 mp4Options 一旦被评估为应该能够“不存在”是类型never。作为 ab 实验,我也尝试让它回退到未定义。如果我手动分配mp4Options: undefined了这行得通,否则类型检查器仍然抱怨缺少属性。我认为绝对不应该是这种情况,因为我们可以省略undefined开箱即用的属性(没有条件类型)。

是否有解决方法或不那么复杂的方法?还是我的代码中有错误?

标签: typescripttype-inferenceconditional-types

解决方案


我认为使用通用基类为 and 派生两个单独的类可能会MediaFile更好。mp4png

如果您确实想通过有条件的魔术路线进入单节课,我们可以做到。尽管条件类型不能像您想要的那样影响属性的可选性,但我们可以将它们与交集类型结合以获得所需的效果:

// just for convenience
type MP4OptionsT = {
    codec?: 'h264',
    profile: 'baseline' | 'main' | 'high',
    bitrate: number,
};
type FormatOptions<F extends 'mp4' | 'png'> = (F extends 'mp4' ? { mp4Options: MP4OptionsT } : { mp4Options?: never})

class MediaFile<Format extends 'mp4' | 'png'> {
    public path: string;
    public format: Format // no need for a conditional type here, it the same type as Format

    public mp4Options: FormatOptions<Format>['mp4Options'];

    constructor(opts: {
        path: string,
        format: Format,
    } &  FormatOptions<Format>)
    {
        this.path = opts.path;
        this.format = opts.format;
        this.mp4Options = opts.mp4Options;
    }
}

// this is OK, no need for explicit type arguments
const mp4File = new MediaFile({
    path: '/some/file/somewhere.mp4',
    format: 'mp4',
    mp4Options: {
        profile: 'high',
        bitrate: 1000,
    }
});
mp4File.mp4Options.bitrate // ok 

// no need for the type argument 
const pngFile = new MediaFile({
    path: '/some/file/somewhere.png',
    format: 'png', // no need for mp4Options
});
pngFile.mp4Options.codec // error

推荐阅读