[TypeScript] Interface and Class
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的更多相关文章
- TypeScript Interface vs Types All In One
TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...
- TypeScript Interface(接口)
类型检查专注于解析值所具有的"形态",这是TypeScript的核心原则之一.这个有时候被称为"duck typing"或者"structural s ...
- 被迫开始学习Typescript —— interface
一开始以为,需要使用 class 来定义呢,学习之后才发现,一般都是使用 interface 来定义的. 这个嘛,倒是挺适合 js 环境的. 参考:https://typescript.bootcss ...
- typescript interface 泛型
interface interface Obj { [index: string]: any; } class Person { name: string; } let obj: obj = { na ...
- react: typescript interface useState issue
define interface: interface ILoginState { imageId: string; imageSrc: string; username: string; passw ...
- 【区分】Typescript 中 interface 和 type
在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...
- Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)
概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loa ...
- TypeScript之接口类型
Interfaces 作为TypeScript中的核心特色之一,能够让类型检查帮助我们知道一个对象应该有什么,相比我们在编写JavaScript的时候经常遇到函数需要传递参数,可能在编写的时候知道这个 ...
- [TypeScript ] What Happens to Compiled Interfaces
This lesson covers using your first TypeScript Interface and what happens to the Interface when it i ...
随机推荐
- html ---- a 标签 在新窗口打开的问题
- Android实现QQ分享及注意事项
一.获取APPID和帮助文档 在前面我介绍了关于Android中微信分享的文章< Android实现微信分享及注意事项>这一篇文章来看看关于QQ分享. 可以参看新手引导和接入说明:http ...
- 针对CDP协议攻击分析及安全防护
针对CDP协议攻击分析及安全防护 熟悉Cisco的朋友都知道CDP协议是思科公司独特的发现协议,在思科公司出产的所有路由器和交换机里面都能运行此协议,一台运行C D P的路由器或交换机能够得知与它直接 ...
- CTF编程题-三羊献瑞(实验吧)解题随记
题目如下.解题步骤参考的是https://cloud.tencent.com/developer/news/373865中作者的思路. 1.首先,两个四位数相加等于一个五位数,那么这个五位数的第一位必 ...
- CMDB学习之一
CMDB - 配置管理数据库 资产管理 自动化相关的平台(基础 CMDB): 1. 发布系统 2. 监控 3. 配管系统.装机 4. 堡垒机 CMDB的目的: 1. 替代EXCEL资产管理 —— 资产 ...
- 图片工具GraphicsMagick的安装配置与基本使用
本文使用GraphicsMagick的版本为1.3.18 (Released March 9, 2013). 1.简介 GraphicsMagick是一个短小精悍的的图片处理工具和库集合.对于Java ...
- js--27门面模式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Ajax : $. get()和$.post() $.getScript $.getJSON
<body> <input type="button" value="Ajax" /> <div id="box&quo ...
- FineUI 页面跳转
要加 EnableAjax=false; <f:Button ID="btn1" EnableAjax="false" OnClick="btn ...
- codeforces 1037E. Trips(倒叙)
题目传送门: 解题思路: 正着搞好像有点恶心. 反着搞. 一边删一边搞,从崩坏的地方开始,入度--. 最后dfs崩坏,更新答案. 注意要把边删掉防止重复崩坏. 代码: #include<cstd ...