簡體   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