interface

作者: 苍老师的眼泪 | 来源:发表于2022-08-25 20:15 被阅读0次

定义 interface 类型和用 type 定义类型别名是类似的,注意 interface 定义类型不需要等号:

type person = {
    name: string,
    age: number,
    alias?: string,
}

interface people {
    name: string,
    age: number,
    alias?: string,
}

// 向现有结构添加属性

interface people {
    gender: 0 | 1
}

如果是 type 的话,会提示重复定义

接口继承接口

interface Animal {
    gender: 0 | 1
}

interface Colorful {
    color: string
}

// 很显然 ts 支持多重扩展
interface Human extends Animal, Colorful {
    speak: () => void
}

let edison: Human = {
    gender: 1,
    speak() {
        console.log('Are you ok?')
    },
    color: 'blue'
}

相关文章

  • interface{} 与 []interface{}

    简介 基于可以对interface{}赋值任何类型的变量,很多人会尝试如下的代码: 很不幸会导致错误: 于是问题来...

  • Interface

    Interface is a group of method signatures, continuin with...

  • Interface

    为什么interface中的变量都是static public final? 首先接口是一种高度抽象的"模版",,...

  • interface

    接口用于定义对象的 '轮廓' 但是如果我们直接传入一个对象字面量进去,ts就会报错, 因为ts对对象字面量检测更加...

  • interface

    http://blog.csdn.net/justaipanda/article/details/43155949...

  • @interface

    深入理解Java:注解(Annotation)自定义注解入门http://www.cnblogs.com/peid...

  • Interface

    4.1 Comma Separated Value. *Comma Separated Value *or CSV...

  • interface

    由于基础很差不太明白interface到底是个啥接口上我一直不太理解,插上就通电了......通电什么意思 插曲最...

  • interface

    java语言里面使用interface来声明一个接口,接口其实是一个特殊的抽象类,在接口里面的方法全部都是抽象的。...

  • @Interface

    1.@Retention:RetentionPolicy.SOURCE:检查,源文件存在,编译丢失Retentio...

网友评论

      本文标题:interface

      本文链接:https://www.haomeiwen.com/subject/uttogrtx.html