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. Elasticsearch之shield(权限)插件安装之后的浏览详解

    前期博客 Elasticsearch-2.4.3的3节点安装(多种方式图文详解)(含 head.kopf.marvel.shield和watcher插件安装和使用) 访问es:-u es_admin ...

  2. 图片工具GraphicsMagick的安装配置与基本使用

    本文使用GraphicsMagick的版本为1.3.18 (Released March 9, 2013). 1.简介 GraphicsMagick是一个短小精悍的的图片处理工具和库集合.对于Java ...

  3. 洛谷 P3385 【模板】负环

    P3385 [模板]负环 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M ...

  4. WordPress出现Briefly unavailable for scheduled maintenance. Check back in a minute. 的解决方法

    WordPress出现 Briefly unavailable for scheduled maintenance. Check back in a minute. 解决方法: 登入FTP,然后把Wo ...

  5. [BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP

    链接 题意:给你一些区间,每个区间都有一个花费,求覆盖区间 \([S,T]\) 的最小花费 题解 先将区间排序 设 \(f[i]\) 表示决策到第 \(i\) 个区间,覆盖满 \(S\dots R[i ...

  6. [51Nod]NOIP2018提高组省一冲奖班模测训练(四)翻车记+题解

    链接 下午5点的时候,突然想起来有这个比赛,看看还有一个小时,打算来AK一下,结果因为最近智商越来越低,翻车了,我还是太菜了.上来10分钟先切掉了C和A,结果卡在了B题,唉. A.砍树 一眼题,两遍树 ...

  7. vue.js有什么用,是用来做什么的(整理)

    vue.js有什么用,是用来做什么的(整理) 一.总结 一句话总结:用数据绑定的思想,vue可以简单写单个页面,也可以写一个大的前端系统,也可以做手机app的界面. 1.Vue.js是什么? 渐进式框 ...

  8. Cisco安全防护读书笔记之一Cisco系统设备协议漏洞

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  9. Node中的JavaScript和浏览器中的JavaScript的区别

    浏览器中的JavaScript: 1.基于ECMAscript规范,这个规范规定了语法 2.添加了dom:用来处理文档 document object model 3.添加了BOM:用于操作浏览器 w ...

  10. 安装Mysql最新版本mysql-5.7.10-winx64出现的几个问题解决

    电脑是64位的安装不了Windows (x86, 32-bit),Mysql installer MSI ,然后下载了Windows (x86, 32-bit), ZIP Archive 这种是免安装 ...