ahwwh
ahwwh

Reputation: 37

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

vscode intellisense doesn't work when union types have character and string type.

type t = 't1' | 't2';

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

var a: t = 't1';

example: https://i.sstatic.net/BheRk.jpg

Upvotes: 3

Views: 1873

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249716

Typescript does simplifications on unions and intersections. One of these simplifications is base types absorb subtypes. string is the base type of all string literal types (such as 't1' and 't2') This means as far as the compiler is concerned type t = 't1' | 't2' | string; is just a fancy way to write string.

This GitHub issue documents this exact problem and the proposed workaround should work for you as well:

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

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

Upvotes: 4

Related Questions