打字稿从三元条件推断字符串文字

typescript infer string literal from ternary conditional

这是一个简化的例子:

function doSomething(animal: 'bird' | 'fish'){ }

let flies=true;

const animal = flies ? 'bird' : 'fish'

doSomething(animal);         

Typescropt 推断类型 'bird' | 'fish' 从三元条件赋值给动物。 (如果 animal 不是 const 它会抱怨,因为它会推断类型字符串不能分配给 'bird' | 'fish')

但是

const parms ={
    animal: flies ? 'bird' : 'fish'
}
doSomething(parms);  /* Argument of type '{ animal: string; }' is not    
                        assignable to parameter of type '{ animal: "bird" | "fish"; } */

这里是从三元条件推断字符串。有没有办法保持这种风格(即不必定义类型并将野外动物声明为该类型)

Typescript 仅在特定情况下推断字符串文字类型。 属性 通常不是这些情况之一,除非有额外的情况暗示 属性 的文字类型。 (它与三元运算符无关)。

在 Typescript 3.4 中(在撰写本文时尚未发布,但在 npm 中已作为 typescript@next 提供)您将能够提示编译器您希望根据 this 问题:

let flies=true;
//types as  { readonly animal: "bird" | "fish"; }
const parms ={
    animal: flies ? 'bird' : 'fish'
} as const

在 3.3 及以下版本中,您可以使用一个函数来告诉编译器您想要推断出文字类型:

let flies=true;
function withLiteralTypes<T extends Record<string, P>, P extends string | number | null | boolean | Record<string, P>> (o: T) {
    return o;
}
// types as { animal: "bird" | "fish"; }
const parms =withLiteralTypes({
    animal: flies ? 'bird' : 'fish',
})