Skip to content

Commit f3811bc

Browse files
committed
feat: update articles
1 parent 1c5ff54 commit f3811bc

2 files changed

Lines changed: 42 additions & 25 deletions

File tree

TypeScript/类型使用/基础类型.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ enum StoryType {Video = 10, Article = 20, Tutorial=30}
8282

8383
```ts
8484
enum Colors {
85-
Red = 'RED',
86-
Green = 'GREEN',
87-
Blue = 'BLUE'
85+
Red = "RED",
86+
Green = "GREEN",
87+
Blue = "BLUE"
8888
}
8989
```
9090

@@ -93,8 +93,8 @@ enum Colors {
9393
在 TypeScript 中,我们能够创建 Typed Arrays 或者 Generic Arrays,Typed Arrays 的创建方式如下:
9494

9595
```ts
96-
const tags: string[] = ['javascript', 'programming'];
97-
tags.push('typescript');
96+
const tags: string[] = ["javascript", "programming"];
97+
tags.push("typescript");
9898
tags.forEach(function(tag) {
9999
console.log(`Tag ${tag}`);
100100
});
@@ -114,7 +114,7 @@ interface StringArray {
114114
}
115115

116116
let myArray: StringArray;
117-
myArray = ['Bob', 'Fred'];
117+
myArray = ["Bob", "Fred"];
118118

119119
let myStr: string = myArray[0];
120120
```
@@ -160,7 +160,7 @@ interface NestedArray extends Array<NestedArray | Atom> { }
160160
type AtomOrArray = Atom | NestedArray;
161161

162162
// Usage
163-
let foo: AtomOrArray = [ "", 1, [1, 2, ""] ] let bar: AtomOrArray = ""
163+
let foo: AtomOrArray = ["", 1, [1, 2, ""] ] let bar: AtomOrArray = ""
164164
```
165165

166166
## Tuple
@@ -169,9 +169,9 @@ TypeScript 同时提供了 Tuple 元组类型,允许返回包含不同的已
169169

170170
```js
171171
let storyTitles = [
172-
'Learning TypeScript',
173-
'Getting started with TypeScript',
174-
'Building your first app with TypeScript'
172+
"Learning TypeScript",
173+
"Getting started with TypeScript",
174+
"Building your first app with TypeScript"
175175
];
176176

177177
let titlesAndLengths: [string, number][] = storyTitles.map(function(title) {
@@ -207,13 +207,13 @@ never 类型的典型应用场景,就是处理函数中可能的不可达代
207207

208208
```ts
209209
function foo(x: string | number): boolean {
210-
if (typeof x === 'string') {
210+
if (typeof x === "string") {
211211
return true;
212-
} else if (typeof x === 'number') {
212+
} else if (typeof x === "number") {
213213
return false;
214214
}
215215

216-
return fail('Unexhaustive!');
216+
return fail("Unexhaustive!");
217217
}
218218
```
219219

@@ -242,7 +242,7 @@ let foo: unknown = 10;
242242

243243
function hasXYZ(obj: any): obj is { x: any; y: any; z: any } {
244244
return (
245-
!!obj && typeof obj === 'object' && 'x' in obj && 'y' in obj && 'z' in obj
245+
!!obj && typeof obj === "object" && "x" in obj && "y" in obj && "z" in obj
246246
);
247247
}
248248

@@ -268,6 +268,6 @@ TypeScript 允许我们使用 any 关键字来描述不确定的类型,编译
268268

269269
```ts
270270
let notSure: any = 4;
271-
notSure = 'maybe a string instead';
271+
notSure = "maybe a string instead";
272272
notSure = false; // okay, definitely a boolean
273273
```

TypeScript/类型使用/类与接口.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
TypeScript 中的类与其他面向对象的编程语言非常相似,即类是此类(对象)实例所支持的内容的契约定义;此外,类还可以从其他类甚至接口继承功能。不过 TypeScript 中的类仅支持单个构造函数。
44

5+
# interface(接口)
6+
7+
```ts
8+
export declare function keys<T extends object>(): Array<keyof T>;
9+
10+
import { keys } from "ts-transformer-keys";
11+
12+
interface Props {
13+
id: string;
14+
name: string;
15+
age: number;
16+
}
17+
const keysOfProps = keys<Props>();
18+
19+
console.log(keysOfProps); // ['id', 'name', 'age']
20+
```
21+
522
#
623

724
从 ES6 开始,JavaScript 内建支持使用 class 关键字来声明类,而 TypeScript 允许我们以 implements 来实现某个接口,或者以 extends 关键字来继承某个类:
@@ -12,7 +29,7 @@ class Child extends Parent implements IChild, IOtherChild {
1229
property: Type;
1330

1431
// 类属性默认值
15-
defaultProperty: Type = 'default value';
32+
defaultProperty: Type = "default value";
1633

1734
// 私有属性
1835
private _privateProperty: Type;
@@ -67,7 +84,7 @@ class TextStory implements Story {
6784
}
6885

6986
// 使用静态方法创建类对象
70-
let story = TextStory.storyWithNoTags('Learning TypeScript');
87+
let story = TextStory.storyWithNoTags("Learning TypeScript");
7188

7289
class TutorialStory extends TextStory {
7390
constructor(title: string, ...tags) {
@@ -173,7 +190,7 @@ export class Entity {
173190
## Task
174191

175192
```ts
176-
import { Entity } from './entity';
193+
import { Entity } from "./entity";
177194

178195
export class Task extends Entity {
179196
private _completed: boolean;
@@ -204,8 +221,8 @@ export class Task extends Entity {
204221
我们将创建的第三个类是 Story,一个代表用户故事的具体类。故事可以细分为多个任务以方便其执行,但只有一个人负责故事及其任务。除此之外,故事包含一个标题(继承自实体)和一个标识故事是否已经完成的标志。要定义 Story 类,让我们使用以下代码在./src 目录中创建一个名为 story.ts 的文件:
205222

206223
```ts
207-
import { Entity } from './entity';
208-
import { Task } from './task';
224+
import { Entity } from "./entity";
225+
import { Task } from "./task";
209226

210227
export class Story extends Entity {
211228
private _completed: boolean;
@@ -256,8 +273,8 @@ export class Story extends Entity {
256273
我们将创建的第四个也是最后一个类将是 Project 类。项目包含零个或多个故事,可以在完成后发布,并且可以具有标题(从实体继承)。要定义此类,让我们在./src 目录中创建一个名为 project.ts 的文件,并添加以下代码:
257274

258275
```ts
259-
import { Entity } from './entity';
260-
import { Story } from './story';
276+
import { Entity } from "./entity";
277+
import { Story } from "./story";
261278

262279
export class Project extends Entity {
263280
private _released: boolean;
@@ -303,8 +320,8 @@ export interface Completable {
303320
定义此接口后,我们可以使用它来限制哪些对象可以传递给发送电子邮件的函数。让我们在./src 目录中创建一个名为 index.ts 的文件,以查看此操作:
304321

305322
```ts
306-
import { Task } from './task';
307-
import { Completable } from './completable';
323+
import { Task } from "./task";
324+
import { Completable } from "./completable";
308325

309326
function sendCompletionEmail(completable: Completable) {
310327
if (!completable.completed) {
@@ -318,7 +335,7 @@ function sendCompletionEmail(completable: Completable) {
318335
// ...
319336
}
320337

321-
let bugFix = new Task(1, 'Weirdo flying bug');
338+
let bugFix = new Task(1, "Weirdo flying bug");
322339
sendCompletionEmail(bugFix);
323340
bugFix.completed = true;
324341
sendCompletionEmail(bugFix);

0 commit comments

Comments
 (0)