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: 8700
};
let superVillain: ComicBookCharacter = {secretIdentity: "YuLong",alias: "YuLong",health: 9150
};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: 9000,strength: 5000,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: 9000,strength: 5000,attack: attackFunc
};
let superVillain: ComicBookCharacter = {secretIdentity: "YuLong",alias: "YuLong",health: 7600,insanity: 200,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相关推荐

  1. [TypeScript] 编程实践之1: Google的TypeScript代码风格2:基本概念

    TypeScript语言规范 2 基本概念 2.1 语法约定 2.2 命名 2.2.1 保留字 2.2.2 属性命名 2.2.3 计算属性命名 2.3 声明 2.4 范围 2 基本概念 本文档的其余部 ...

  2. typescript 方法后面加感叹号_使用 TypeScript 模板字面类型

    介绍 在今天的早些时候,Anders Hejlsberg 在 TypeScript 的仓库中发了一个 Pull Request:Template string types and mapped typ ...

  3. typescript的基本结构_上帝视角看 TypeScript

    点击蓝色"脑洞前端"关注我哟 加个"星标",带你揭开大前端的神秘面纱! ❝ 这是脑洞前端第「99」篇原创文章 TypeScript 的学习资料非常多,其中也不乏 ...

  4. typeScript面试必备之-通识七:typeScript中的可索引接口(数组,对象)+类类型接口...

    可索引接口:数组.对象的约束 (不常用) ts定义数组的方式 var arr:number[]=[2342,235325]var arr1:Array<string>=['111','22 ...

  5. [TypeScript] 编程实践之1: Google的TypeScript代码风格3:类型

    TypeScript语言规范 3 类型 3.1 Any类型 3.2 基本类型 3.2.1 Number类型 3.2.2 Boolean类型 3.2.3 String类型 3.2.4 Symbol类型 ...

  6. TypeScript深度剖析:Vue项目中应用TypeScript?

    一.前言 与link类似 在VUE项目中应用typescript,我们需要引入一个库vue-property-decorator, 其是基于vue-class-component库而来,这个库vue官 ...

  7. 【TypeScript】(一)快速上手TypeScript类型语法

    文章目录 1.概述 2.TS中的类型 2.1.类型推断 2.2.类型定义 2.3.复杂类型 2.3.1.联合 2.3.2.泛型 2.4.type关键字 3. 总结 1.概述 TypeScript(TS ...

  8. typescript用什么软件写_用TypeScript写了个低配版H5美图工具

    前言 最近两月在学习canvas时候,发现很多有意思的技术能力,特别是在图像处理这一领域.让我想起大学课堂教学的<数字图像处理>(冈萨雷斯 版本).但是很遗憾的是,大学上完课应付考试后全部 ...

  9. 什么是TypeScript?为什么我要用它代替JavaScript? [关闭]

    本文翻译自:What is TypeScript and why would I use it in place of JavaScript? [closed] Closed . 已关闭 . This ...

最新文章

  1. 孩子听不进道理怎么办?
  2. 关于数据中心基础架构管理
  3. Java和Python安装和编译器使用
  4. UML中关联,聚合,组合的区别及C++实现
  5. php实现第三方邮箱登录_JavaScript实现第三方登录网站原理在这呢
  6. 原创 | 万万没想到,JVM内存结构的面试题可以问的这么难?
  7. 2015 - Human-level control through deep reinforcement learning
  8. 电阻电导传输线的归一化转移矩阵[a](必背)
  9. HTMLCSS入门学习
  10. aix如何查看日志策略_AIX系统日志学习笔记之一
  11. python代码混淆工具_Intensio-Obfuscator:一款专业Python代码混淆处理工具
  12. Android 超级工具类
  13. 参考文献在论文中进行引用标注
  14. Ubuntu1804 使用mondorescue 进行系统备份iso制作
  15. 手机电池-市场现状及未来发展趋势
  16. 园区元宇宙:打造智慧园区综合治理可视化管理平台
  17. 常用的adodb使用方法
  18. 盘点PDF文件转Word文档的四种高效率转换方法
  19. 如何利用开源插件?又快又好地搞好数据接口开发,连通不同应用系统
  20. buuoj Pwn writeup 166-170

热门文章

  1. 读于丹《趣品人生》有感
  2. Spring框架学习day_01: 框架配置方式/ 管理对象的作用域/ 生命周期/ 组件扫描/ 单例模式:“懒汉式“,“饿汉式“
  3. python多个线程join_python-使用`thread.join()`时多线程冻结
  4. C语言指针总结大学霸IT达人
  5. java 字符串拼接优化_JAVA字符串拼接效率
  6. css多行超出显示点_CSS实现单行、多行文本溢出显示省略号(…)
  7. mysql不具有的特征是_MySQL中的SQL特征(转)
  8. uni上传图片跨域_uni-app的项目实践心得
  9. python画有权重网络图_python networkx 根据图的权重画图实现
  10. vue树形结构html,怎么在vue中利用递归组件实现一个树形控件