📖
blog
  • README
  • JavaScript
    • 元素的宽高位置信息梳理
    • dom-align 源码浅析
    • Event Loop
    • 函数实参为对象时的陷阱
    • export 与 utils 方法书写规范
    • 手写 Promise 及相关代码理解
    • call,apply,bind 等函数的模拟实现
    • JavaScript继承
    • JavaScript 数据类型与类型判断
    • for..of 和 for..in 的区别
    • 写给自己看的 next 函数
    • JS 可选链与双问号
    • mouseenter 与 mouseover 事件的区别
    • Immutable相关知识
  • CSS
    • 不简单的 z-index
    • 两列布局,三列布局
    • CSS 居中方案整理
    • CSS 像素,设备像素,2倍图梳理
    • iconfont 的使用
  • Node JS
    • 实现简易的 express
  • React 核心知识点整理
    • 高阶组件
    • React 事件处理
    • React Hooks
    • React Context
  • React 状态管理
    • Redux 基础概念
    • Redux 中间件和异步操作
    • Redux Saga
    • Redux 只能有一个 store 对象嘛
  • React 开发实践
    • Ant Design Menu 组件的使用与深入
    • 讲讲吸顶效果与 react sticky
    • 基于 express,搭建 react 的开发环境
    • 通过 antd input 组件分析受控与非受控组件
    • DebounceClick 组件
    • react component Align 组件分析
    • React Portal 之事件冒泡
    • React Transition Group 源码浅析
    • React.cloneElement 父组件向子组件注入 props
    • 一次 Align 组件的问题记录
    • 如何知道子组件的类型
    • React Router 源码简单分析
    • React Redux 源码简单分析
  • Vue.js
    • Vue.js 概览
    • scoped 样式中的 deep
  • TypeScript 语法
    • 基础类型
    • 变量声明
    • 接口
    • 类
    • 函数
    • 泛型
    • 枚举
    • 类型推论
    • 类型兼容性
    • 高级类型
    • Symbol
    • 迭代器和生成器
    • 模块
    • 命名空间
    • JSX
  • 玩转 webpack
    • 第一章: webpack 与构建发展简史
    • 第二章:webpack基础用法
    • 第三章:webpack进阶用法
    • 第四章:编写可维护的 webpack 构建配置
    • 第五章:webpack构建速度和体积优化策略
    • 第六章:通过源代码掌握webpack打包原理
    • 第七章:编写Loader和插件
  • webpack 实践
    • 如何配置 output.library
  • 测试
    • 初识代码测试
    • Jest 中 如何测试 setTimeout
    • Jest Enzyme React 测试实践记录
  • WEB 开发,过往工作沉淀
    • Web安全(DVWA)
    • 内存泄露与事件移除的必要性
    • url to pdf api 与 服务部署踩坑记录
    • 前端调试指南
    • Markdown 转 email
    • github travis ci 自动部署
    • 浏览器缓存知识梳理
    • WEB 系统登录相关知识梳理
    • 将-Axios-请求参数和返回值进行格式化
    • source-map与源码调试
    • HTTPS
    • 使用 rollup 打造自己的 npm 包 (全流程)
    • father-build 是如何工作的
  • 书籍
    • 图解 HTTP 协议
    • 编写可维护的 JavaScript
    • 鸟哥的 Linux 私房菜
    • JavaScript Promise迷你书
  • Linux
    • vimtutor
    • CURL 使用指南
  • Nginx
    • 一次 nginx 分享
  • Git
    • Git Commit Message 须知
    • .gitignore 模板
    • git tag标签
  • 摄影
    • 摄影基础知识
    • 手机摄影从小白到大师
  • 翻译
    • log4js
    • log4js-node
    • 介绍GitLab上的CI/CD
    • 为GitLab Pages创建并调整GitLab CI/CD
    • 关于 rel=noopener
    • AngularJS 团队 Git 提交信息约定
    • JSON Schema
  • Lifehack
    • 20 个 Google 搜索 Tips 来高效使用 Google
    • 37 个高级 Google 搜索 Tips
Powered by GitBook
On this page

Was this helpful?

  1. TypeScript 语法

接口

在Typescript里,接口的作用就是为这些类型命名和为你的代码或者第三方代码定义契约。

interface LabelType {
    label: string;
}
function printLabel(labelObj: LabelType) {
    console.log(labelObj.label)
}

可选属性

interface Label {
    label: string;
    width?: number;
}

只读属性

interface Point {
    readonly x: string;
}

const a: Point = {x:'1'}

// cannot assign to 'x' because it is a read-only property.
a.x = '2';

额外的属性检查

interface A {
    a?: string; // 注意是可选的属性
}
function f(arg: A) {

}

f({b:1})  // error

即对象字面量会被特殊对待而且会经过额外属性检查,当将他们赋值给变量作为参数传递时,如果对象字面量存在任何“目标类型”不包含的属性时,将会得到一个错误。

在本例中,属性b不存在于目标类型A中。

如何绕开检查: 1. 类型断言

f({b:1} as A)
  1. 字符串索引签名

    interface A {
     a?: string;
     [propKey:string]: any;
    }

    这里表示的是, A 可以有任意数量的属性。

函数类型 接口除了描述带有属性的普通对象外,还可以描述函数类型:

interface Fun {
    (source:string, subString: string): boolean
}

let myFun: Fun;
myFun = function (s1, s2) {
    return s1 > s2
}

可索引类型

interface StringArray {
    [key: number]: string;
}
let myArray: StringArray;
myArray = ['1','2']

TypeScript支持两种索引签名,字符串和数字,但是数字索引的返回值必须是字符串索引返回值类型的子类型。

class Animal {
    name: string;
}
class Dog extends Animal {
    breed: string;
}
interface NotOkay {
    // error, 数字索引返回Animal,不是字符串索引Dog的子类型
    [x: number]: Animal;
    [x: string]: Dog;
}

interface NotOkay {
    // right, 数字索引返回 Dog,是字符串索引 Animal 的子类型
    [x: string]: Animal;
    [x: number]: Dog;
}

字符串索引签名会确保所有属性与其返回值类型匹配。

interface NumberDictionary {
    [key: string]: number,
    length: number;
    name: string  //  error
}

上述例子要求当key为字符串时,返回值必须为number类型,而 name 不符合

类类型

强制类去符合某种契约

// 要求类的实例必须有 currentTime 属性,以及 setTime 方法
interface ClockInterface {
    currentTime: Date,
    setTime(d: Date);
}

class Clock implements ClockInterface {
    currentTime: Date;
    setTime(d: Date) {
        this.currentTime = d;
    }
    constructor(h: number, m: number) {}
}

类静态部分与实例部分的区别

// 构造器签名
interface ClockConstructor {
    new (hour: number, minute: number);
}

class Clock implements ClockConstructor {
    currentTime: Date;
    constructor(h: number, m: number) { }
}

当一个类实现一个接口时,只能对实例的部分进行类型检查,constructor存在于类的静态部分,不在类的检查范围内。

继承接口

interface Shape {
    color: string;
}

interface Square extends Shape {
    sideLength: number;
}

let square = <Square>{};  // 同时有 color 属性和 sideLength 属性
square.color = "blue";
square.sideLength = 10;

继承多个接口

interface Shape {
    color: string;
}

interface PenStroke {
    penWidth: number;
}

interface Square extends Shape, PenStroke {
    sideLength: number;
}

let square = <Square>{};
square.color = "blue";
square.sideLength = 10;
square.penWidth = 5.0;
Previous变量声明Next类

Last updated 4 years ago

Was this helpful?