[TypeScript] Typescript Intersection & Union Types
amptypescript types union intersection
2023-09-14 08:59:12 时间
Union type: means "one of" those types
Intersection type: means "combination" of those types
Intersection types
type typeAB = typeA & typeB;
interface ErrorHandling {
success: boolean;
error?: { message: string };
}
interface ArtworksData {
artworks: { title: string }[];
}
interface ArtistsData {
artists: { name: string }[];
}
// These interfaces are composed to have
// consistent error handling, and their own data.
type ArtworksResponse = ArtworksData & ErrorHandling;
type ArtistsResponse = ArtistsData & ErrorHandling;
const handleArtistsResponse = (response: ArtistsResponse) => {
if (response.error) {
console.error(response.error.message);
return;
}
console.log(response.artists);
};
function makeWeek(): Date & { end: Date } {
const start = new Date()
const end = new Date(start.valueOf() + ONE_WEEK)
return { ...start, end } // kind of Object.assign
}
const thisWeek = makeWeek()
thisWeek.toISOString()
thisWeek.end.toISOString()
Union types
let varName = typeA | typeB;
interface Bird {
fly(): void;
layEggs(): void;
}
interface Fish {
swim(): void;
layEggs(): void;
}
declare function getSmallPet(): Fish | Bird;
let pet = getSmallPet();
pet.layEggs();
// ERROR:
// Only available in one of the two possible types
pet.swim();
相关文章
- [Typescript] Convert a js project to typescript
- [AWS] RTO & RPO
- [Typescript] Dynamic types: Use TypeScript's Mapped Types and Template Literal Types Together
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [Typescript v4] Tuple Types && Recursive types
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
- 备份&添加无线网络配置
- Maven中<dependencies>节点和<dependencyManagement>节点的区别
- [Typescript] Get a TypeScript package ready for release to NPM
- [Typescript] Step1 & 2 for converting a js app to ts
- [Typescript] Extract & Exclude
- [Typescript] Type Queries (keyof & typeof)
- [TypeScript] Typescript Intersection & Union Types
- [React Typescript 2022] Type a Radio Button Component in React using TypeScript
- [Typescript] Dynamic types: Use TypeScript's Mapped Types and Template Literal Types Together
- [Typescript] Statically Type Unknown Values with TypeScript's unknown Type
- [Typescript] Extend Functionality of a TypeScript Class with Decorators
- [Typescript] Simplify iteration of custom data structures in TypeScript with iterators (backwards iteration with for ... of.. loop)
- [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers
- [Typescript] Generics using TypeScript
- [Typescript] Sorting arrays in TypeScript
- 打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 <serviceDebug> 配置行为)以便将异常信息发送回
- 算法设计 - 前缀和 & 差分数列
- Visual Studio Code的下载安装&设置中文包/配置中文语言
- 基于蒙特卡洛法的规模化电动车有序充放电及负荷预测(Python&Matlab实现)
- Guava中基础工具类Joiner的使用&字符串拼接方法 joiner.on
- 杭州电ACM1098——Ignatius's puzzle
- 专利申请流程细节问答(Q & A)
- 【C++要笑着学】Functor 仿函数 | 模拟实现 stack & queue | 模拟实现优先级队列