When to use Interface and when to use Class.

Let's see one example:

export interface Lesson {
courseId: string;
description: string;
duration?: string;
longDescription?: string;
tags: string | string[];
url?: string;
videoUrl: string;
} export class Lesson {
constructor(
public courseId: string,
public description: string,
public duration: string,
public longDescription: string,
public tags: string | string[],
public url: string,
public videoUrl: string) {
}
}

We have an Interface 'Lesson' and a Class 'Lesson'. At this point, I would love to say, there is no differece between using interface or using Class. Actually I prefer Interface in this case, because its short-hand syntax.

We when you want to add more functionalities, you need to using Class instead of Interface.

For example:

export class Lesson {
constructor(public $key: string,
public courseId: string,
public description: string,
public duration: string,
public longDescription: string,
public tags: string | string[],
public url: string,
public videoUrl: string) {
} get hasVideoUrl() {
return !!this.videoUrl;
} get hasMultiTags() {
if (this.tags instanceof Array) {
return true;
} else {
return false;
}
} static fromJsonList(array): Lesson[] {
return array.map(Lesson.fromJson);
} static fromJson({
$key,
courseId,
description,
duration,
longDescription,
tags,
url,
videoUrl
}): Lesson {
return new Lesson($key,
courseId,
description,
duration,
longDescription,
tags,
url,
videoUrl)
}
}

We add two getter and setter, hasMuliTags and hasVideoUrl. Basiclly we add two more props into the class dynamically based on other props.

'fromJson' & 'formJsonList' are two static function, which helps to stucture our Lesson instance, in Angualr2 we can use like this:

// Service

@Injectable()
export class CourseService {
lessons$: FirebaseListObservable<Lesson[]>; constructor(private rt: RealtimeService) {
this.lessons$ = rt.getLessonObs();
} getLessons() {
return this.lessons$
.map(Lesson.fromJsonList);
}
}

[TypeScript] Interface and Class的更多相关文章

  1. TypeScript Interface vs Types All In One

    TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...

  2. TypeScript Interface(接口)

    类型检查专注于解析值所具有的"形态",这是TypeScript的核心原则之一.这个有时候被称为"duck typing"或者"structural s ...

  3. 被迫开始学习Typescript —— interface

    一开始以为,需要使用 class 来定义呢,学习之后才发现,一般都是使用 interface 来定义的. 这个嘛,倒是挺适合 js 环境的. 参考:https://typescript.bootcss ...

  4. typescript interface 泛型

    interface interface Obj { [index: string]: any; } class Person { name: string; } let obj: obj = { na ...

  5. react: typescript interface useState issue

    define interface: interface ILoginState { imageId: string; imageSrc: string; username: string; passw ...

  6. 【区分】Typescript 中 interface 和 type

    在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...

  7. Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)

    概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loa ...

  8. TypeScript之接口类型

    Interfaces 作为TypeScript中的核心特色之一,能够让类型检查帮助我们知道一个对象应该有什么,相比我们在编写JavaScript的时候经常遇到函数需要传递参数,可能在编写的时候知道这个 ...

  9. [TypeScript ] What Happens to Compiled Interfaces

    This lesson covers using your first TypeScript Interface and what happens to the Interface when it i ...

随机推荐

  1. css选择器和优先级总结

    本文转自http://www.cnblogs.com/zxjwlh/p/6213239.html CSS三大特性—— 继承. 优先级和层叠. 继承:即子类元素继承父类的样式; 优先级:是指不同类别样式 ...

  2. 闲的无聊写了个很(wu)有(liao)意(dao)思(bao)的程序

    下午机房断网了 闲的无聊,写了个小游戏 忘了sleep在哪个库里了.. 自带变色效果哦 #include<iostream> #include<cstdio> #include ...

  3. Codeforces Round #196 (Div. 2) 少部分题解

    A:sort以后求差值最小 ]; int main() { int n,m; cin>>n>>m; ; i < m ; i++) cin>>a[i]; sor ...

  4. 【2017 Multi-University Training Contest - Team 3】RXD and math

    [Link]: [Description] [Solution] 发现1010mod(109+7)=999999937; 猜测答案是nk 写个快速幂; 注意对底数先取模; [NumberOf WA] ...

  5. Gym - 100502A Amanda Lounges

    Amanda Lounges Time Limit: 1000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u AMAN ...

  6. ABAP调用外部WebService

    TCode:se80 选择 Package,输入我们自己的开发包,后回车 右击 开发包名称,选择菜单 出现创建向导窗体 选择"Service Consumer",点击 继续 选择& ...

  7. js19--继承终极版本

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  8. Arrays.asList()方法的限制

    Arrays.asList()方法的限制是他对所产生的List类型做出了最理想的假设 package example; import java.util.Arrays; import java.uti ...

  9. 18.C语言多线程总结

    创建一个线程 _beginthread(myfun, , NULL);//返回值是一个HANDLE hd[i] = CreateThread(NULL, , add, NULL, , NULL);// ...

  10. weblogic虚拟路径配置

    首发地址 https://blog.leapmie.com/archives/344/ 前言 weblogic的虚拟路径配置有两种: 一种是在项目下配置,即在weblogic.xml中配置,该方法配置 ...