当前位置:网站首页>TypeScript-类型别名
TypeScript-类型别名
2022-06-11 07:54:00 【YY小怪兽】
详情可见
1.什么是类型别名?
类型别名就是给一个类型起个新名字, 但是它们都代表同一个类型
例如: 你的本名叫张三, 你的外号叫小三, 小三就是张三的别名, 张三和小三都表示同一个人
1.给string类型起了一个别名叫做MyString, 那么将来无论是MyString还是string都表示string
type MyString = string;
let value:MyString;
value = 'abc';
value = 123;
value = false;
2.类型别名也可以使用泛型
type MyType<T> = {
x:T, y:T};
let value:MyType<number>;
value = {
x:123, y:456};//因为T是number,里面只能是number
value = {
x:'123', y:456};//报错
value = {
x:false, y:456};//报错
3.在类型别名类型的属性中使用自己
type MyType = {
name:string,
// 一般用于定义一些树状结构或者嵌套结构
children?:MyType
}
let value:MyType = {
name:'one',
children:{
name:'one',
children:{
name:'one',
}
}
}
4.接口和类型别名是相互兼容的
type MyType = {
name:string
}
interface MyInterface {
name:string
}
let value1:MyType = {
name:'lnj'};
let value2:MyInterface = {
name:'zs'};
value1 = value2;
value2 = value1;
边栏推荐
猜你喜欢

安卓初中级开发基础知识整理(面试自用)

How to prepare for the new PMP syllabus exam?

C language to achieve a simple game - minesweeping

用 Keras/TensorFlow 2.9 创建深度学习模型的方法总结

SOCKET【5】- struct linger 用法

(transformation) tree, binary tree and forest transformation principle
![[atcoder1980] mystious light (mathematical simulation)](/img/c0/7de31b36e11ff71328d927c1d1c2d3.png)
[atcoder1980] mystious light (mathematical simulation)

Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np

2022.6.7 special student simulation

The solution of "no startup device" after running Bochs
随机推荐
VIM common commands
Modular linear equations (Chinese remainder theorem + general solution)
Bladed入门教程(视频)
forEach 中 return 和 for 中 break
[poj3691] DNA repair (AC automata +dp)
二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...
运筹学导论
Sort - merge sort
Space geometry
[atcoder2306] rearranging (topology)
【IoT】项目管理:如何打造更好的跨职能团队?
2022.6.6 extra long growth simulation
String Simulation Implementation
Note: JDBC
[atcoder1981] short diameter (graph theory thinking)
Remote office experience sharing | community essay solicitation
[atcoder1980] mystious light (mathematical simulation)
批量拼接字符串
Use of wordcloud
Scrape captures 51job Recruitment Information (static page)