typeScript interface和type区别
typeScript interface和type区别
interface 参考资料 ----> https://www.tslang.cn/docs/handbook/interfaces.html
// ts 自定义类型/*** Interface(接口) vs Type alias(类型别名)* 相同点-> 都可以描述一个对象或者函数* -> 都允许拓展(extends)* --->> interface extends interface (接口继承接口)* --->> type extends type (类型继承类型)* --->> interface extends type (接口继承类型)* --->> // type extends interface (类型继承接口)*/
// interface
interface UserOP { // 描述一个对象name: string;email: string;isBig: boolean;age: number;
}interface SetUser { // 描述一个函数(name: string, email: string, isBig: boolean, age: number): UserOP;
}let pikaqiu: UserOP; // 创建一个变量是 UserOP的类型
pikaqiu = { name: 'zyn', email: 're', isBig: false, age: 34 }let mySearchXX: SetUser; // 用来描述一个方法
mySearchXX = function (name: string, email: string, isBig: boolean, age: number): UserOP {return pikaqiu;
}// typetype UserTy = {name: stringage: number
};type SetUserTy = (name: string, age: number) => void;let pikaqiu1: UserTy;
pikaqiu1 = { name: 'zyn', age: 54 };let mySearchXXTy: SetUserTy;
mySearchXXTy = function (name: string, age: number) {}// interface extends interface (接口继承接口)// 1. interface 属性继承
interface dudu1 {name: string
}interface dudu2 extends dudu1 {age: number
}const duduTest: dudu2 = { name: 'zyb', age: 23 };// type extends type (类型继承类型)type Nametype = {name: string;
}
type UserType = Nametype & { age: number };
const valueType: UserType = { name: 'zyb', age: 23 };// interface extends type (接口继承类型)type LulvwaType = {name: string
}interface LulvwaFace extends LulvwaType {age: number;
}const LulvwaValue: LulvwaFace = { name: 'zyb', age: 23 };// type extends interface (类型继承接口)interface shajFace {name: string
}type shajType = shajFace & {age: number;
}
const shajValue: shajType = { name: 'zyb', age: 23 };/*** Interface(接口) vs Type alias(类型别名)* 不同点-> type 可以而 interface 不行* -----> type 可以声明基本类型别名,联合类型,元组等类型* -> interface 可以而 type 不行* -----> interface 能够声明合并*/// 基本类型别名
type DiffName = string;// 联合类型
interface Dog {wong();
}
interface Cat {miao();
}type Pet = Dog | Cat// 元祖类型
type PetList = [Dog, Pet]// interface 能够声明合并interface DiffLx {name: string
}
interface DiffLx {age: number
}
interface DiffLx {sex: string
}// 合并后的结果
/*
interface DiffLx {name: stringage: numbersex: string
}
*/
const DiffLxValue: DiffLx = { name: '34', age: 34, sex: 'nv' }
typeScript interface和type区别相关推荐
- TypeScript 里 interface 和 type 的区别
StackOverflow 上的讨论链接 Interface vs Type alias in TypeScript 2.7 Differences Between Type Aliases and ...
- TypeScript中interface 与 type的区别,你真的懂吗?
在写 ts 相关代码的过程中,总能看到 interface 和 type 的身影.它们的作用好像都一样的,相同的功能用哪一个都可以实现,也都很好用,所以也很少去真正的理解它们之间到底有啥区别, 分别在 ...
- typescript和 java区别_typescript中interface和type的区别
相同点 都可以描述一个对象或者函数 interface interface User { name: string age: number } interface SetUser { (name: s ...
- 三分钟了解interface和type的区别
对typescript 有一定了解的,会发现 interface 和 type 很相似,类型定义上,很多时候,用两种方式都能实现. 三分钟直入主题, 除了语法不同外,interface和type主要有 ...
- TypeScript中的interface、type、class——泰联病从口入
先看效果 type 和 interface 有什么异同? 1.interface侧重于描述数据结构,type侧重于描述类型 interface A{name:string; } type B = 'b ...
- TypeScript interface
typescript之旅 1.TypeScript-Basic 2.TypeScript interface 3.Typescript-module(1) 4.TypeScript Modules(2 ...
- 【进阶】TypeScript 中的 Type
一 .什么是 TypeScript TypeScript 是静态编程语言 , 是 JavaScript 的超集 简而言之:JavaScript 有的 TypeScript 都有.JavaScript ...
- TypeScript杂谈(一)——type关键字
type的定义 type:其作用就是给类型起一个新名字,可以作用于原始值(基本类型),联合类型,元组以及其它任何你需要手写的类型 type Second = number; // 基本类型 let t ...
- 抽象类(abstract class)和接口(Interface)的区别
抽象类(abstract class)和接口(Interface)的区别 前言 抽象类(abstract class)和接口(Interface)是Java语言中对于抽象类定义进行支持的两种机制,赋予 ...
最新文章
- ReentrantLock+线程池+同步+线程锁
- 资源 | 25个机器学习面试题,期待你来解答
- Linux文件系统2---VFS的四个主要对象
- python做动态折线图_Python数据可视化 pyecharts实现各种统计图表过程详解
- npm 删除依赖包_前端开发,npm会了吗?
- python读写修改配置文件(ini)
- linux服务器curl,在Linux系统下使用curl自定义HTTP头的方法
- VTK:PolyData之DetermineArrayDataTypes
- 016、JVM实战总结:大厂面试题:JVM中有哪些垃圾回收算法,每个算法各自的优劣?
- 用户登陆过程 及时更新信息方法
- 面试常被问的65个问题及回答技巧(请收藏)
- 关于使用UE编辑器无法初始化ftp的问题
- 【转载】Altera FPGA使用通用SPI Flash(代替EPCS的方法)
- 电磁铁轭铁和极头收缩角
- 负载均衡——三角传输
- 多元微积分_旋度2.旋度公式推导
- 七彩虹 iGame G-ONE Plus 27寸游戏一体机 评测
- matlab求自相关频率,使用自相关求周期性
- 四川省天府新区知识产权信息公共服务网点申报好处条件材料
- Dazdata BI之PDF魔幻输出