import { A } from "https://example.com/type.d.ts";
type B = A | A

Playground

如果您在TS Playground和VS Code中都将鼠标悬停在B上,它将在shell type B = A中显示为A,在shell type B = A | A中显示为any.不管A是否已知,我希望它们应该是一致的.为什么不是这样呢?

推荐答案

根据microsoft/TypeScript#54630,特别是this comment by the TS team dev lead:

对于当程序中存在错误类型时会发生什么,没有具体的保证.

当你导入一些找不到的东西时,你会得到一个编译器错误:

import { A } from "https://example.com/type.d.ts"; // error!
// -------------> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Cannot find module 'https://example.com/type.d.ts' or its corresponding type declarations.

任何对它的引用都将显示为TypeScript想要显示的任何形式. 它通常会显示为the any typebecause

悬停显示"任何",因为缺少更好的显示.它可以被区分,但这些东西往往不会在程序中停留很长时间.

这就是为什么你看到any sometimes.


至于为什么它显示A而不是any时,你从

type B = A | A
//   ^? type B = any

type C = A;
//   ^? type C = A

there's no documented and stable rule by which such types are displayed. Assuming that A is sometimes displayed as any, then it's really the compiler's prerogative 到 decide which one of those it wants 到 display. Even if A weren't an error, you'd still have such a dicho到my:

type D = { a: string };

type E = D;
//   ^? type E = { a: string; }

type F = D[];
//   ^? type F = D[];

There you have type D, which is clearly an alias for {a: string}. Some uses of D get reduced 到 {a: string}, while others stay as D.

These differences are only how the type gets displayed 到 the user, and shouldn't affect the actual identities of the types. There are no supported official mechanism by which a developer can just tell the compiler which form she would prefer 到 see when displaying a type. There are various open feature requests like microsoft/TypeScript#28508, microsoft/TypeScript#31940, and microsoft/TypeScript#45954, which could possibly change that if they were implemented, but for now it's not part of the language. The compiler uses heuristics 到 determine which is the type 到 display; these heuristics perform well enough in a wide range of use cases, but it is necessarily the case that they cannot meet everyone's needs simultaneously. For every person who wants D 到 be displayed as {a: string}, there is an equal and opposite person who wants it do be displayed as D, and they both have very important reasons for it 到 be that way, and both think their way would make things "consistent". More developer control would be nice, but without that, you just get what you get.

So: I can't easily say for certain why you see any for A | A but A for A, without stepping through the TypeScript compiler code and maybe finding pull requests that document the parts responsible. But even if I did that, it wouldn't be particularly enlightening, and it could easily change in a future version of TypeScript. As long as two types are identical, in some sense it's completely up 到 the compiler 到 decide which one 到 show and when.

Playground link 到 code

Typescript相关问答推荐

创建一个将任意命名类型映射到其他类型的TypScript通用类型

将对象属性路径描述为字符串数组的类型

Angular -使用Phone Directive将值粘贴到控件以格式化值时验证器不工作

在TypScript手册中可以视为接口类型是什么意思?

react 路由6操作未订阅从react 挂钩表单提交+也不使用RTK查询Mutations

无法从Chrome清除还原的初始状态数据

TypeScrip省略了类型参数,仅当传递另一个前面的类型参数时才采用默认类型

函数重载中的不正确TS建议

垫表页脚角v15

在列表的指令中使用intersectionObservable隐藏按钮

重载函数的T helper参数

类型脚本-处理值或返回值的函数

将布尔值附加到每个对象特性

如何解决&Quot;类型不可分配给TypeScrip IF语句中的类型&Quot;?

react 路由不响应参数

如何创建内部和外部组件都作为props 传入的包装器组件?

如何通过转换器类继承使用多态将对象从一种类型转换为另一种类型

类型分布在第一个泛型上,但不分布在第二个泛型上

定义一个只允许来自另一个类型的特定字符串文字的类型的最佳方式

Typescript 从泛型推断类型