[TypeScript] Using Interfaces to Describe Types in TypeScript
It’s easy to pass the wrong value to a function. Typescript interfaces are great because they catch errors at compile time or in an IDE. In this lesson we’ll learn how to describe a type shape with Typescript interfaces.
Using interface to describe an object:
- interface ComicBookCharacter {
- secretIdentity?: string;
- alias: string;
- health: number;
- }
- let superHero: ComicBookCharacter = {
- alias: 'Zero',
- health:
- };
- let superVillain: ComicBookCharacter = {
- secretIdentity: "YuLong",
- alias: "YuLong",
- health:
- };
- function getSecretIdentity(character: ComicBookCharacter){
- if(character.secretIdentity){
- console.log(`${character.alias} is ${character.secretIdentity}`);
- } else {
- console.log(`${character.alias} has no secret identity`);
- }
- }
- getSecretIdentity(superHero);
Using interface to describe an function:
- interface AttackFunction {
- (opponent: {alias: string; health: number}, attackWith: number): number;
- }
- interface ComicBookCharacter {
- secretIdentity?: string;
- alias: string;
- health: number;
- }
- function attackFunc(opponent, attackWith){
- opponent.health -= attackWith;
- console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`);
- return opponent.health;
- }
- let superHero: ComicBookCharacter = {
- alias: 'Zero',
- health: ,
- strength: ,
- attack: attackFunc
- };
Using extends: Using extends of an interface is clean way to define interface in typescript.
- interface OptionalAttributes {
- strength?: number;
- insanity?: number;
- dexterity?: number;
- healingFactor?: number;
- }
- interface ComicBookCharacter extends OptionalAttributes{
- secretIdentity?: string;
- alias: string;
- health: number;
- attack: AttackFunction;
- }
Code:
- interface AttackFunction {
- (opponent: {alias: string; health: number}, attackWith: number): number;
- }
- interface KrustyTheClown {
- alias: string;
- health: number;
- inebriationLevel: number;
- attack: AttackFunction;
- }
- interface OptionalAttributes {
- strength?: number;
- insanity?: number;
- dexterity?: number;
- healingFactor?: number;
- }
- interface ComicBookCharacter extends OptionalAttributes{
- secretIdentity?: string;
- alias: string;
- health: number;
- attack: AttackFunction;
- }
- function attackFunc(opponent, attackWith){
- opponent.health -= attackWith;
- console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`);
- return opponent.health;
- }
- let superHero: ComicBookCharacter = {
- alias: 'Zero',
- health: ,
- strength: ,
- attack: attackFunc
- };
- let superVillain: ComicBookCharacter = {
- secretIdentity: "YuLong",
- alias: "YuLong",
- health: ,
- insanity: ,
- attack: attackFunc
- };
- function getSecretIdentity(character: ComicBookCharacter){
- if(character.secretIdentity){
- console.log(`${character.alias} is ${character.secretIdentity}`);
- } else {
- console.log(`${character.alias} has no secret identity`);
- }
- }
- superHero.attack(superVillain, superHero.strength); //"Zero attacked YuLong, who's health = 2600"
[TypeScript] Using Interfaces to Describe Types in TypeScript的更多相关文章
- [TypeScript] Using Assertion to Convert Types in TypeScript
Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the c ...
- 在 Typescript 2.0 中使用 @types 类型定义
在 Typescript 2.0 中使用 @type 类型定义 基于 Typescript 开发的时候,很麻烦的一个问题就是类型定义.导致在编译的时候,经常会看到一连串的找不到类型的提示.解决的方式经 ...
- [TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...
- [TypeScript] Query Properties with keyof and Lookup Types in TypeScript
The keyof operator produces a union type of all known, public property names of a given type. You ca ...
- [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...
- [TypeScript] Model Alternatives with Discriminated Union Types in TypeScript
TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of a ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript
Conditional types take generics one step further and allow you to test for a specific condition, bas ...
- [TypeScript] Define Custom Type Guard Functions in TypeScript
One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a ...
随机推荐
- BootStrap_Table 学习
https://blog.csdn.net/heting90/article/details/52248729 $("#realTime_Table").bootstrapTabl ...
- 51Nod 飞行员配对(二分图最大匹配)(匈牙利算法模板题)
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
- deep-in-es6(三)
模板字符串:反撇号(`)包起来的内容. eg: var str = `assassin`; console.log(str); 模板占位符:${};可达到数据的渲染,在占位符中可以是表达式,运算符,函 ...
- 前台技术--div的隐藏与显示
怎样使用页面元素隐藏或显示. HTML为我们提供了两个变量visibility和display visibility:隐藏要元素可是元素所暂用的空间不予释放.也就是说元素隐藏了,可是页面上会流出一片空 ...
- Android启动原理剖析
我们知道Android是以一个Activity为单位的,可是我们并没有看到一个Activity是怎么開始启动的. 今天我 们就从Android的源码開始讲吧. ActivityThread: Andr ...
- 「微信小程序」有哪些冲击与机会?
昨天晚上相信大家的朋友圈被「微信小程序」刷屏了,这影响力赶上了国务院出台新政策一样,足以说明微信在中国的影响力之大. 然后今天公号后台一大堆人问我怎么看这件事,不少人非常忧虑,仿佛自己将要失业一样. ...
- 36.Node.js 工具模块--OS模块系统操作
转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js os 模块提供了一些基本的系统操作函数.我们可以通过以下方式引入该模 ...
- Linux下vi替换字符命令操作实例
在Linux下的开发中,经常涉及到对文件里的字符进行处理,当中,对字符的替换操作也是非常的频繁. 本文以一个实际的文件为例,具体介绍了Linux下经常使用的vi替换字符命令,为相关的开发工作提供给了參 ...
- Android抖动的EditText
Java代码:启动动画 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); findViewById(R.id.pw ...
- 用电脑从Google Play下载apk
用电脑从Google Play下载apk 方法一:给Chrome浏览器安装apk-downloader插件,需禁止 SSL 错误警告,即在Chrome的快捷方式上加入"--ignore-ce ...