繁体   English   中英

如何覆盖 TypeScript 类型别名的 VSCode 工具提示?

[英]How to override VSCode tooltip for TypeScript type alias?

以下代码不起作用:

type char = 'a' | 'b' | 'c' | 'd' | 'e' | 'f';
const s: string = 'foo';
const [c]: char = s;
// [ERROR]: Type 'string' is not assignable to type 'char'.

我可以这样做:

type char = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | string;

但这会导致两个问题:

  1. 当我在char变量上 hover 时,工具提示说它的类型是string而不是char (我在 VSCode 中)。 我知道我没有'a' | 'b' | 'c' 'a' | 'b' | 'c' 'a' | 'b' | 'c'等...在屏幕截图中,但假装我这样做,因为工具提示是相同的。

    工具提示

  2. 如果我尝试做类似const letter: char = 'notachar'事情,我不会收到警告;

如何覆盖 TypeScript 类型别名的 VSCode 工具提示?

当您有一个可以解决更简单问题的联合时, TypeScript 将始终显示更简单的版本

例子:

type char = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | string;
// resolves to the following simpler type so that is what TypeScript will show. 
type char = string;

暂无
暂无

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

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