函数类型接口
// 接口
interface Add {
(a: number, b: string): number;
}
// 实现具体的函数
let add: Add = (a, b) => a + b;
混合类型接口
因为 JavaScript 其动态灵活的特点,有时你会希望一个对象可以同时具有上面提到的多种类型。
interface Lib {
(): void; // 函数
version: string; // 属性
dosomething(): void; // 方法
}
function getLib(version: string) {
let lib = (() => {}) as Lib;
lib.version = version || "1.0";
lib.doSomething = () => {
console.log("do something");
};
return lib;
}
let l = getLib("2.0");
l();
l.version;
l.doSomething();
类接口
TypeScript 基础系列
- Hello TypeScript(01)-- 环境搭建
- Hello Typescript(02)-- 枚举类型
- Hello Typescript(03)-- 对象类型接口
- Hello Typescript(04)-- 函数类型接口、混合类型接口、类接口
- Hello Typescript(05)-- 函数
- Hello Typescript(06)-- 类
- Hello Typescript(07)-- 类与接口的关系
- Hello Typescript(08)-- 泛型
- Hello Typescript(09)-- 类型推断、类型兼容性、类型保护
- Hello Typescript(10)-- 交叉类型、联合类型、索引类型、映射类型、条件类型