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:

  1. interface ComicBookCharacter {
  2. secretIdentity?: string;
  3. alias: string;
  4. health: number;
  5. }
  6.  
  7. let superHero: ComicBookCharacter = {
  8. alias: 'Zero',
  9. health:
  10. };
  11. let superVillain: ComicBookCharacter = {
  12. secretIdentity: "YuLong",
  13. alias: "YuLong",
  14. health:
  15. };
  16.  
  17. function getSecretIdentity(character: ComicBookCharacter){
  18. if(character.secretIdentity){
  19. console.log(`${character.alias} is ${character.secretIdentity}`);
  20. } else {
  21. console.log(`${character.alias} has no secret identity`);
  22. }
  23. }
  24.  
  25. getSecretIdentity(superHero);

Using interface to describe an function:

  1. interface AttackFunction {
  2. (opponent: {alias: string; health: number}, attackWith: number): number;
  3. }
  4.  
  5. interface ComicBookCharacter {
  6. secretIdentity?: string;
  7. alias: string;
  8. health: number;
  9. }
  10.  
  11. function attackFunc(opponent, attackWith){
  12. opponent.health -= attackWith;
  13. console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`);
  14. return opponent.health;
  15. }
  16.  
  17. let superHero: ComicBookCharacter = {
  18. alias: 'Zero',
  19. health: ,
  20. strength: ,
  21. attack: attackFunc
  22. };

Using extends: Using extends of an interface is clean way to define interface in typescript.

  1. interface OptionalAttributes {
  2. strength?: number;
  3. insanity?: number;
  4. dexterity?: number;
  5. healingFactor?: number;
  6. }
  7.  
  8. interface ComicBookCharacter extends OptionalAttributes{
  9. secretIdentity?: string;
  10. alias: string;
  11. health: number;
  12. attack: AttackFunction;
  13. }

Code:

  1. interface AttackFunction {
  2. (opponent: {alias: string; health: number}, attackWith: number): number;
  3. }
  4.  
  5. interface KrustyTheClown {
  6. alias: string;
  7. health: number;
  8. inebriationLevel: number;
  9. attack: AttackFunction;
  10. }
  11.  
  12. interface OptionalAttributes {
  13. strength?: number;
  14. insanity?: number;
  15. dexterity?: number;
  16. healingFactor?: number;
  17. }
  18.  
  19. interface ComicBookCharacter extends OptionalAttributes{
  20. secretIdentity?: string;
  21. alias: string;
  22. health: number;
  23. attack: AttackFunction;
  24. }
  25.  
  26. function attackFunc(opponent, attackWith){
  27. opponent.health -= attackWith;
  28. console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`);
  29. return opponent.health;
  30. }
  31.  
  32. let superHero: ComicBookCharacter = {
  33. alias: 'Zero',
  34. health: ,
  35. strength: ,
  36. attack: attackFunc
  37. };
  38. let superVillain: ComicBookCharacter = {
  39. secretIdentity: "YuLong",
  40. alias: "YuLong",
  41. health: ,
  42. insanity: ,
  43. attack: attackFunc
  44. };
  45.  
  46. function getSecretIdentity(character: ComicBookCharacter){
  47. if(character.secretIdentity){
  48. console.log(`${character.alias} is ${character.secretIdentity}`);
  49. } else {
  50. console.log(`${character.alias} has no secret identity`);
  51. }
  52. }
  53.  
  54. superHero.attack(superVillain, superHero.strength); //"Zero attacked YuLong, who's health = 2600"

[TypeScript] Using Interfaces to Describe Types in TypeScript的更多相关文章

  1. [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 ...

  2. 在 Typescript 2.0 中使用 @types 类型定义

    在 Typescript 2.0 中使用 @type 类型定义 基于 Typescript 开发的时候,很麻烦的一个问题就是类型定义.导致在编译的时候,经常会看到一连串的找不到类型的提示.解决的方式经 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [TypeScript] Understand lookup types in TypeScript

    Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. BootStrap_Table 学习

    https://blog.csdn.net/heting90/article/details/52248729 $("#realTime_Table").bootstrapTabl ...

  2. 51Nod 飞行员配对(二分图最大匹配)(匈牙利算法模板题)

    第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...

  3. deep-in-es6(三)

    模板字符串:反撇号(`)包起来的内容. eg: var str = `assassin`; console.log(str); 模板占位符:${};可达到数据的渲染,在占位符中可以是表达式,运算符,函 ...

  4. 前台技术--div的隐藏与显示

    怎样使用页面元素隐藏或显示. HTML为我们提供了两个变量visibility和display visibility:隐藏要元素可是元素所暂用的空间不予释放.也就是说元素隐藏了,可是页面上会流出一片空 ...

  5. Android启动原理剖析

    我们知道Android是以一个Activity为单位的,可是我们并没有看到一个Activity是怎么開始启动的. 今天我 们就从Android的源码開始讲吧. ActivityThread: Andr ...

  6. 「微信小程序」有哪些冲击与机会?

    昨天晚上相信大家的朋友圈被「微信小程序」刷屏了,这影响力赶上了国务院出台新政策一样,足以说明微信在中国的影响力之大. 然后今天公号后台一大堆人问我怎么看这件事,不少人非常忧虑,仿佛自己将要失业一样. ...

  7. 36.Node.js 工具模块--OS模块系统操作

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js os 模块提供了一些基本的系统操作函数.我们可以通过以下方式引入该模 ...

  8. Linux下vi替换字符命令操作实例

    在Linux下的开发中,经常涉及到对文件里的字符进行处理,当中,对字符的替换操作也是非常的频繁. 本文以一个实际的文件为例,具体介绍了Linux下经常使用的vi替换字符命令,为相关的开发工作提供给了參 ...

  9. Android抖动的EditText

    Java代码:启动动画 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); findViewById(R.id.pw ...

  10. 用电脑从Google Play下载apk

    用电脑从Google Play下载apk 方法一:给Chrome浏览器安装apk-downloader插件,需禁止 SSL 错误警告,即在Chrome的快捷方式上加入"--ignore-ce ...