vscode 当联合类型有字符和字符串类型时,智能感知不工作

vscode intellisense donnot work when union types have character and string type

vscode 当联合类型有字符和字符串类型时,智能感知不起作用。

type t = 't1' | 't2';

var a: t = 't1';
type t = 't1' | 't2' | string;

var a: t = 't1';

示例:https://imgur.com/n1Q7aNl

Typescript 对并集和交集进行了简化。这些简化之一是基本类型吸收子类型。 string 是所有字符串文字类型(例如 't1''t2')的基本类型,这意味着就编译器而言,type t = 't1' | 't2' | string; 只是一种奇特的编写方式string.

这个 GitHub issue 记录了这个确切的问题,建议的解决方法也应该适用于您:

type t = 't1' | 't2' | (string &  { fromT?: any});

var a: t = 'ty'; //ok
var a: t = 't2'; //ok, with autocomplete