简体   繁体   English

Typescript从三元条件推断字符串文字

[英]typescript infer string literal from ternary conditional

This is a simplified example: 这是一个简化的示例:

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

let flies=true;

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

doSomething(animal);         

Typescropt infers type 'bird' | Typescropt推断类型为“ bird” | 'fish' in the assignation to animal from the ternary conditional. 从三元条件到动物的“鱼”。 (if animal weren't const it would complain as it would infer type string not being assignable to 'bird' | 'fish') (如果动物不是const,它将抱怨,因为它将推断类型字符串不能分配给'bird'|'fish')

But

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

Here it's infering string from the ternary conditional. 这里是从三元条件推断字符串。 Is there a way to keep things in this style (ie. not having to define a type and declaring the field animal as that type) 有没有办法让事物保持这种风格(即,不必定义类型并将场动物声明为该类型)

Typescript only infers string literal types in certain situation. Typescript仅在某些情况下推断字符串文字类型。 A property is unusually not one of those cases unless there are extra circumstances to hint a literal type for the property. 除非有额外的情况提示该属性的文字类型,否则属性不是其中一种情况。 (it has nothing to do with the ternary operator). (与三元运算符无关)。

In Typescript 3.4 (unreleased at the time of writing, but already available as typescript@next in npm ) you will be able to hint the compiler that you want object literals inferred as per this issue: 在Typescript 3.4(在撰写本文时尚未发布,但已经在npm以typescript typescript@next的形式提供)中,您将能够提示编译器您希望根据问题推断对象文字:

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

In 3.3 and below you could use a function to tell the compiler you want a literal type inferred: 在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',
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM