[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 ...
随机推荐
- MyBatis映射
mybatis-config.xml映射文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- lastlog---显示系统中所有用户最近一次登录信息。
lastlog命令用于显示系统中所有用户最近一次登录信息. lastlog文件在每次有用户登录时被查询.可以使用lastlog命令检查某特定用户上次登录的时间,并格式化输出上次登录日志/var/log ...
- Xcode 6 打包ipa文件
随着Xcode6.1的普遍应用.随之而来的非常多问题就会出现.这里来说一下怎样在Xcode6.1上生成Ad-hoc ipa.首先是要在你的开发人员账号上生成一个.ipa的主要应用就是在你公布到AppS ...
- php课程 12-39 继承中parent的作用是什么
php课程 12-39 继承中parent的作用是什么 一.总结 一句话总结:PHP5中使用parent::来引用父类的方法.parent:: 可用于调用父类中定义的成员方法. parent::的追溯 ...
- index action分析
上一篇从结构上分析了action的,本篇将以index action为例仔分析一下action的实现方式. 再概括一下action的作用:对于每种功能(如index)action都会包括两个基本的类* ...
- Office Open XML导出大数据
Office Open XML导出大量数据到 Excel .NET使用Office Open XML导出大量数据到 Excel我相信很多人在做项目的都碰到过Excel数据导出的需求,我从最开始使用最原 ...
- Google Web Toolkit(GWT) 在windows下环境搭建
1.什么是GWT? Google Web Toolkit(简称GWT,读作/ˈɡwɪt/),是一个前端使用JavaScript,后端使用Java的AJAX framework,以Apache许可证2. ...
- wampserver安装后访问localhost出现 Forbidden问题
Forbidden You don't have permission to access / on this server. 修改php的配置文件httpd.conf. 在原有的位置文件中找到配置节 ...
- hibernate 的映射文件快速生成:使用CodeSmith快速生成映射文件和映射类
一 CodeSmith简介 本文以表自动生成NHibernate的映射文件和映射类的实例来说明一下本软件的使用方法. CodeSmith是一种基于模板的代码生成工具,其使用类似于ASP.NET的语法来 ...
- 强大的xUtils工具类整理
xUtils简单介绍 xUtils 包括了非常多有用的android工具. xUtils 支持大文件上传,更全面的http请求协议支持(10种谓词),拥有更加灵活的ORM,很多其它的事件注解支持且不受 ...