Home > Back-end >  how to define a type enum some string value, the others are any string?
how to define a type enum some string value, the others are any string?

Time:10-22

I can get the vscode tip with number:

enter image description here

but when union with string failed:

enter image description here

CodePudding user response:

The canonical way to do this is to intersect string with {}. string normally would absorb string literal types in a union (the string literal types are already part of string so in effect they are redundant for type checking)

type color = 'white' | 'black' | (string & {}); 

Playground Link

CodePudding user response:

The 'string' type not working as expected in typescript with union types. You should make it 'String' rather than 'string' and it will work I think.

type color = 'white' | 'black' | String; 
  • Related