[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 ...
随机推荐
- #学习记录#——CSS content 属性
CSS content 属性常结合:before 和:after 这两个伪类一起使用,给指定的元素添加内容来丰富页面. 1. 添加文本内容 html: <h1>给末尾添加内容. </ ...
- vue和miniui 一起使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【Django】认证系统
目录 #. auth模块 1. 认证 authenticate() 2. 登陆 login(HttpRequest, user) 3. 注销 logout(request) 4. 认证判断 is_au ...
- nice---进程优先级
在当前程序运行优先级基础之上调整指定值得到新的程序运行优先级,用新的程序运行优先级运行命令行"command [arguments...]".优先级的范围为-20 - 19 等40 ...
- [TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript
This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types di ...
- Apache-DBUtils包对数据库的操作
•commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装.学习成本极低.而且使用dbutils能极大简化jdbc编码的工作量,同一时候也不会影 ...
- 取消xp开机默认登陆账户
取消xp开机默认登陆账户 建了个新用户,把以前的用户删除后重新启动电脑,始终停留在 "正在启动" 界面,网上说是 Event Log:Eventlog(系统日志纪录服务) 没有自动 ...
- Spring中提供的集合工具类util CollectionUtils
转自:https://blog.csdn.net/fangwenzheng88/article/details/78457850 CollectionUtils类 /* * Copyright 200 ...
- css 兼容性前缀
一.不同浏览器内核下的书写规则 二:transform 具体变性中心基点 transform-origin 默认情况下 rotate旋转.scale缩放.translate位移.矩阵matri ...
- 提高IIS的FTP安全性 管理员的九阴真经
提高IIS的FTP安全性 管理员的九阴真经 <九阴真经>是很多武林高手蒙昧以求的武林秘籍,在系统管理员这个武林中也有很多类似<九阴真经>一样的秘籍.在这里就向大家介绍一下有关提 ...