From fc4e38cb45020fba2ea53f907fbfa8458fafb424 Mon Sep 17 00:00:00 2001 From: LeoEatle Date: Sun, 19 Jan 2020 17:17:56 +0800 Subject: [PATCH] add intersection types --- README.md | 1 + SUMMARY.md | 1 + advanced/README.md | 1 + advanced/declaration-merging.md | 2 +- advanced/further-reading.md | 2 +- advanced/intersection-types.md | 45 +++++++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 advanced/intersection-types.md diff --git a/README.md b/README.md index 0fea6cab..362e8024 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ TypeScript 虽然有[官方手册][Handbook]及其[非官方中文版][中文手 - [类与接口](advanced/class-and-interfaces.md) - [泛型](advanced/generics.md) - [声明合并](advanced/declaration-merging.md) + - [交叉类型](advanced/intersections-types.md) - [扩展阅读](advanced/further-reading.md) - [工程](engineering/README.md) - [代码检查](engineering/lint.md) diff --git a/SUMMARY.md b/SUMMARY.md index 5c876ac6..b9a166c9 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -25,6 +25,7 @@ - [类与接口](advanced/class-and-interfaces.md) - [泛型](advanced/generics.md) - [声明合并](advanced/declaration-merging.md) + - [交叉类型](advanced/intersections-types.md) - [扩展阅读](advanced/further-reading.md) - [工程](engineering/README.md) - [代码检查](engineering/lint.md) diff --git a/advanced/README.md b/advanced/README.md index f969babf..3948e023 100644 --- a/advanced/README.md +++ b/advanced/README.md @@ -10,6 +10,7 @@ - [类与接口](class-and-interfaces.md) - [泛型](generics.md) - [声明合并](declaration-merging.md) +- [交叉类型](advanced/intersections-types.md) - [扩展阅读](further-reading.md) --- diff --git a/advanced/declaration-merging.md b/advanced/declaration-merging.md index f0d50dcd..d4a1b02f 100644 --- a/advanced/declaration-merging.md +++ b/advanced/declaration-merging.md @@ -99,4 +99,4 @@ interface Alarm { --- - [上一章:泛型](generics.md) -- [下一章:扩展阅读](further-reading.md) +- [下一章:交叉类型](intersections-types.md) diff --git a/advanced/further-reading.md b/advanced/further-reading.md index 7121ddf9..0b49de0b 100644 --- a/advanced/further-reading.md +++ b/advanced/further-reading.md @@ -23,5 +23,5 @@ --- -- [上一章:声明合并](declaration-merging.md) +- [上一章:交叉类型](intersections-types.md) - [下一章:工程](../engineering/README.md) diff --git a/advanced/intersection-types.md b/advanced/intersection-types.md new file mode 100644 index 00000000..65f657d9 --- /dev/null +++ b/advanced/intersection-types.md @@ -0,0 +1,45 @@ +# 交叉类型 + +交叉类型(Intersection Types)和联合类型(Union Types)相反,表示该取值的类型属于多个类型的交集,可以使用的属性为这几个类型属性的并集。 + +## 简单的例子 + +```ts +interface A { + a: string +} + +interface B { + b: string +} + +let test: A & B = { + a: 'test a', + b: 'test b' +} +``` + +交叉类型使用 `&` 分隔每个类型。 + +这里 `let test: A & B` 的含义是,`test` 的类型同时属于 `A` 和 `B`,并且同时具有两者的属性 `a` 和 `b`。 + +## 使用联合类型定义 extend object + +```ts +function extend(first: First, second: Second): First & Second { + const result: Partial = { + ...first, + ...second + }; + return result as First & Second; +} +``` + +## 参考 + +- [Advanced Types # Intersection Types](http://www.typescriptlang.org/docs/handbook/advanced-types.html#intersection-types)([中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Advanced%20Types.html#%E4%BA%A4%E5%8F%89%E7%B1%BB%E5%9E%8B)) + +--- + +- [上一章:声明合并](declaration-merging.md) +- [下一章:扩展阅读](further-reading.md)