22
33TypeScript 中的类与其他面向对象的编程语言非常相似,即类是此类(对象)实例所支持的内容的契约定义;此外,类还可以从其他类甚至接口继承功能。不过 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
7289class 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
178195export 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
210227export 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
262279export 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
309326function 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" );
322339sendCompletionEmail (bugFix );
323340bugFix .completed = true ;
324341sendCompletionEmail (bugFix );
0 commit comments