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的更多相关文章

  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. windows WEB 高可用/可伸缩

    windows NLB+ARR实现Web负载均衡高可用/可伸缩   基于IIS的ARR负载均衡 基于NLB负载均衡 这两篇分别分ARR 和 NLB基本配置,下面我们讲讲,如何组合使用,搭配成高可用/可 ...

  2. Zabbix快速入门

    模板的管理与使用 一 模板 模板是zabbix的核心,因为模板继承了所有要监控的内容,以及所要展示的图形等等,zabbix的安装部署完成之后,自带了很多模板(网络设备模板,操作系统模板,常用应用软件模 ...

  3. 写了个去重复文件的 PHP 脚本,

    写了个去重复文件的 PHP 脚本点击打开链接 把各个零散网盘.邮箱和服务器上的文件,三台电脑上的文件收集在新硬盘里,然后清空了网络和电脑上的文件.才发现这个文件不能这里放点,那里存点,到时候不知道在哪 ...

  4. Windows Embedded POSready2009

    Windows Embedded POSready2009 ,这个看上去和 XP 差不多,可能是别人说的 XPE 系统 下载 POSready2009_CD.iso, 安装 KEY :         ...

  5. [NOI.AC#31]MST 计数类DP

    链接 注意到 \(n\) 只有40,爆搜一下发现40的整数拆分(相当于把 \(n\) 分成几个联通块)很少 因此可以枚举联通块状态来转移,这个状态直接用vector存起来,再用map映射,反正40也不 ...

  6. 7.Maven之(七)pom.xml配置文件详解

    转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...

  7. jquery如何阻止子元素相应mouseout事件

    jquery如何阻止子元素相应mouseout事件:mouseout有一个特点,当鼠标移入子元素的时候,也会触发此事件,但是在实际应用中这个特点往往不是我们想要的,下面就通过代码实例介绍一下如何实现此 ...

  8. 关于mybatis里面的Executor--转载

    原文地址:http://blog.csdn.net/w_intercool/article/details/7893344 使用mybatis查寻数据,跟踪其执行流程 最开始执行的语句 this.ge ...

  9. jmeter实现分布式压测步骤

    环境说明:安装与控制机相同版本的jdk与jmeter 1.修改控制机中的jmeter.properties文件 将<remote_hosts=127.0.0.1>改为<remote_ ...

  10. CISP/CISA 每日一题 12

    CISA 每日一题(答) 支付系统模式有哪些: 电子现金模式:支付者不必在线,无条件不可追溯性 电子支票模式:支付者不必在线,涉及个人隐私 电子转帐模式:收款人不必在线 图象处理中,应该有适当的___ ...