Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add intersection types #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [类与接口](class-and-interfaces.md)
- [泛型](generics.md)
- [声明合并](declaration-merging.md)
- [交叉类型](advanced/intersections-types.md)
- [扩展阅读](further-reading.md)

---
Expand Down
2 changes: 1 addition & 1 deletion advanced/declaration-merging.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ interface Alarm {
---

- [上一章:泛型](generics.md)
- [下一章:扩展阅读](further-reading.md)
- [下一章:交叉类型](intersections-types.md)
2 changes: 1 addition & 1 deletion advanced/further-reading.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@

---

- [上一章:声明合并](declaration-merging.md)
- [上一章:交叉类型](intersections-types.md)
- [下一章:工程](../engineering/README.md)
45 changes: 45 additions & 0 deletions advanced/intersection-types.md
Original file line number Diff line number Diff line change
@@ -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, Second>(first: First, second: Second): First & Second {
const result: Partial<First & Second> = {
...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)