简体   繁体   English

TypeScript 判断变量是A型还是B型

[英]TypeScript determine if variable of type A or B

Let's assume I have the following types:假设我有以下类型:

type A = "foo" | "bar"
type B = "abc" | "def"

let x: A | B = "foo"

Right now, as far as I understand, x is of type A |现在,据我了解,x 属于 A 型 | B,乙,

but actually, it has been assigned to be only of type A, right?但实际上,它已被分配为仅 A 类,对吧?

How can I find out that x uses an allowed value of type A without using code like如何在使用类似代码的情况下找出 x 使用 A 类型的允许值

if(x === "foo" || x === "bar") {
    console.log("x is of type A")
}

Because TypeScript is a static type-checker , it only operates at compile time.因为 TypeScript 是一个static 类型检查器,它只在编译时运行。 And because you've informed the compiler that x is of type A | B并且因为您已告知编译器x属于A | B类型。 A | B , then you also need to inform the compiler that (if a certain condition is true), then x can only be of type A . A | B ,那么您还需要通知编译器(如果某个条件为真),那么x只能是A类型。 Therefore, the direct answer to your question is "it's not possible to narrow a type without using a conditional check of some sort".因此,您的问题的直接答案是“如果不使用某种条件检查,就不可能缩小类型”。

A good reference for further exploration is the handbook section on type narrowing .进一步探索的一个很好的参考是关于类型 缩小的手册部分。

TS already knows TS已经知道了

type A = '1';
type B = 2;

const x: A | B = '1';

x.repeat(2); // No error
let x: A | B = "foo"

Right now, as far as I understand, x is of type A |现在,据我了解,x 属于 A 型 | B, but actually, it has been assigned to be only of type A, right? B,但实际上,它已被分配为仅 A 类型,对吗?

x is of type A , which is subset of type A | B x是类型A ,它是类型A | B的子集A | B . A | B So it depends on what type requires your function:所以这取决于什么类型需要你的 function:

const requireAOrB = (value: A | B) => {
 // x is allowed
};

const requireA = (value: A) => {
  // x is allowed
};

const requireB = (value: B) => {
  // x is not allowed
};

x is not of type A and B at the same time , it just fulfills A | B x不是同时属于AB类型,它只是满足A | B A | B superset. A | B超集。

The short answer is you can't.简短的回答是你不能。 TypeScript is compiled to JavaScript and types (at least types like that) do not exist in JavaScript. TypeScript 被编译为 JavaScript 并且类型(至少是这样的类型)在 JavaScript 中不存在。 That means all the types you define are gone at runtime and you have no way of doing a runtime check to determine of which type a variable is.这意味着您定义的所有类型在运行时都消失了,并且您无法进行运行时检查以确定变量的类型。

There are exceptions to this.这也有例外。 For example you can determine at runtime if a variable is a string or number using the typeof keyword:例如,您可以在运行时使用typeof关键字确定变量是string还是number

const x: string | number = 'foo';
if (typeof x === 'string') {
  // do something
}

Another exception are classes.另一个例外是类。 You can check if a variable is an instance of a specific class using the instanceof keyword:您可以使用instanceof关键字检查变量是否是特定 class 的实例:

class A { }
class B { }

const x: A | B = new A();

if (x instanceof A) {
  // do something
}

These things will work as expected and typescript also understands these keywords and will narrow types correctly.这些事情将按预期工作,并且 typescript 也理解这些关键字并正确缩小类型。

For a situation like yours where the types you are using are typescript types that will be omitted at runtime, you really have to compare the values because the types are simply not available at runtime.对于像您这样的情况,您使用的类型是 typescript 类型,将在运行时省略,您确实必须比较这些值,因为这些类型在运行时根本不可用。

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

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