TypeScript联合类型推断失败

时间:2017-09-05 15:27:57

标签: typescript

有时,即使存在类型保护,TypeScript(最新版本)也无法缩小联合类型。 这种行为是错误还是功能:

序言:

// some config
interface Config {
    name: string;
    option1?: number;
    option2?: boolean;
}

// arbitrary type
interface Entity {
    a: number;
    b: number;
}

// name aware type guard for entity property-to-config map
// some default config may be replaced with a property name
type TConfigSet<TData> = {
    [P in keyof TData]: (Config & { name: P }) | P;
}

// example of TConfigSet usage
const EntityConfigs: TConfigSet<Entity> = {
    a: {
        name: 'a',
        option2: true
    },
    b: 'b'
}

问题:

// this function compiles
function TypeLooseFieldToName(name: string | Config): string {
    if (typeof name === 'string') return name;
    else return name.name;
}

// this one doesn't
function TypeStrictFieldToName<TData>(name: keyof TData | { name: keyof TData }): keyof TData {
    if (typeof name === 'string') return name;
    else return name.name; // still keyof TData | { name: keyof TData }, should be shrinked to { name: keyof TData }
}

2 个答案:

答案 0 :(得分:1)

这似乎是类型检查器中的错误,因为TypeScript手册说“keyof T类型被视为string的子类型。”

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html

作为解决方法,可以反转类型保护以首先排除自定义类型:

function hasName(obj: string | { name: string }): obj is { name: string } {
    return typeof obj.name === 'string';
}

function getName<TData>(name: keyof TData | { name: keyof TData }): keyof TData {
    if (hasName(name)) return name.name;
    else return name;
}

// compiles with valid keys
getName<Entity>('a');
getName<Entity>({ name: 'a' });

// doesn't compile with invalid keys
getName<Entity>('z');
getName<Entity>({ name: 'z' });

您可以在GitHub中搜索TypeScript问题并提交新问题(如果以前未解决过这个问题):

https://github.com/Microsoft/TypeScript/issues

答案 1 :(得分:0)

typeof name === 'string'不能用作keyof TData的类型保护,因为编译器显然认为stringkeyof TData是两种不同的类型。如果您为keyof TData添加自己的自定义类型后卫,则可以使用:

function TypeStrictFieldToName<TData>(name: keyof TData | { name: keyof TData }): keyof TData {
    if (isKeyofTData<TData>(name)) return name;
    else return name.name; // type of name here is { name: keyof TData }
}

function isKeyofTData<TData>(name: keyof TData | {}): name is keyof TData {
    return typeof name === 'string';
}