简体   繁体   English

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

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

The following code doesn't work:以下代码不起作用:

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'.

I could do this instead:我可以这样做:

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

But that causes 2 problems:但这会导致两个问题:

  1. When I hover over a char variable, the tooltip says it's type string not char (I'm in VSCode).当我在char变量上 hover 时,工具提示说它的类型是string而不是char (我在 VSCode 中)。 I know I don't have the 'a' | 'b' | 'c'我知道我没有'a' | 'b' | 'c' 'a' | 'b' | 'c' 'a' | 'b' | 'c' etc... in the screenshot, but pretend I do, because tooltip is the same. 'a' | 'b' | 'c'等...在屏幕截图中,但假装我这样做,因为工具提示是相同的。

    工具提示

  2. I won't get warned if I try to do something like const letter: char = 'notachar' ;如果我尝试做类似const letter: char = 'notachar'事情,我不会收到警告;

How to override VSCode tooltip for TypeScript type alias?如何覆盖 TypeScript 类型别名的 VSCode 工具提示?

When you have a union that resolves to something simpler, TypeScript will always show the simpler version当您有一个可以解决更简单问题的联合时, TypeScript 将始终显示更简单的版本

Example:例子:

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