xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeScript Union Types All In One

TypeScript Union Types All In One

vscode 智能提示

// 联合类型 Union Types
let mix: number | string | boolean;
// let mix: string | number | boolean
mix = 1;
mix.toFixed();
mix = 'a';
mix.endsWith('x');
mix = false;
mix.valueOf();


let arr: number[] | string[] | (number | string)[];
arr = [1, 2, 3];
arr = ['a', 'b', 'c'];
arr = [1, 'b', 3];

template union type

// 联合类型 Template Union Types
type Color =  'red' | 'green' | 'blue';


type Country = 'China' | 'USA' | 'England';


// 两个联合类型,按照枚举值自动组合一个新的 联合类 ✅
type CountryColor = `${Country}_${Color}`;
// type CountryColor = "China_red" | "China_green" | "China_blue" | "USA_red" | "USA_green" | "USA_blue" | "England_red" | "England_green" | "England_blue"

TypeScript Cheat Sheets

https://www.cnblogs.com/xgqfrms/p/15877592.html

refs

https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-types

https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-the-in-operator



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-02-06 00:43  xgqfrms  阅读(29)  评论(3编辑  收藏  举报