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 @   xgqfrms  阅读(33)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-02-06 Google 灭霸 彩蛋
2020-02-06 POST 非幂等 All In One
点击右上角即可分享
微信分享提示