当前位置:网站首页>TS基础篇
TS基础篇
2022-07-06 06:58:00 【王昭没有君啊】
注:TS语法中,不加修饰符,就会报错
本文只是介绍一下TS的基本语法,后续会更新TS深层次的内容
1、布尔类型
修饰符 :boolean :Boolean
var time:boolean = true;//限定修饰符小写写
time = new boolean(true);//这种格式在TS中语法中报错
var time3:Boolean = false;//限定修饰符大写
time3 = new Boolean(true);//大写的不报错
2、数值类型
修饰符 :number :Number
var time:number = 1;
var times:Number = 2;
times = new Number(3);
3、字符串类型
修饰符 :string :String
var time:string = "abc";
var times:String = "abcdd";
4、未定义型
修饰符 :undefined
var b:undefined = undefined;
5、或者的使用既可以是number也可以是undefined
var time:number|undefined;
time = 12;
time = undefined;
6、函数
function time(a: number, b? number, c: number = 1):number {
return 12;
}
//以下两种都不会报错
// 不使用默认值
// time(1, 2, 3)
// 使用默认值
time(1,2);
b?number等同于number|undefined
c:number = 1; 默认值
function time():number :number是返回值的类型
function time():void :void就是没有返回值
7、数组类型
7.1、一维数组
var time:Array<number> = [1, 2, 3, 4]; // 只能是数值类型
var time:Array<number|string> = [1 , 2, 3, "a"]; // 既可以是数值类型,又可以是字符串类型
7.2、二维数组
var time:Array<Array<number|string>> = [
[1, 2, "a" ],
[1, 2, "a" ]
]
8、元组类型
// 类型必须和值的顺序相同
var time:[number, string, boolean] = [1, "a", true];
9、枚举类型
类似于对象,其值像常量一样,一旦定义就无法改变,等同于使用对象中的冻结方法
enum COLOR{RED, GREEN, PINK};
console.log(COLOR.RED,COLOR.PINK); // 0,2
enum COLOR{RED="red", GREEN="green", PINK="pink"};
console.log(COLOR.RED, COLOR.PINK); // red, pink
10、null类型
var time:null = null;
11、any 任意类型
不知道给什么类型时,设置为any类型
但是实际工作中不允许使用any类型 禁止使用!!!
因为TS本来就是限制类型的,你用了any都是任意类型了,那么TS也就没什么用了。
var vb:any = 1;
vb = "a";
边栏推荐
- 首发织梦百度推送插件全自动收录优化seo收录模块
- Development of entity developer database application
- leetcode704. 二分查找(查找某个元素,简单,不同写法)
- [brush questions] how can we correctly meet the interview?
- 这个高颜值的开源第三方网易云音乐播放器你值得拥有
- [Yu Yue education] flower cultivation reference materials of Weifang Vocational College
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
- Successfully solved typeerror: data type 'category' not understood
- C language_ Double create, pre insert, post insert, traverse, delete
- A brief introduction of reverseme in misc in the world of attack and defense
猜你喜欢
1189. Maximum number of "balloons"
UWA Pipeline 2.2.1 版本更新说明
Prefix and array series
Introduction to ros2 installation and basic knowledge
云上有AI,让地球科学研究更省力
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
“无聊猿” BAYC 的内忧与外患
Windows Server 2016 standard installing Oracle
Reflex WMS中阶系列3:显示已发货可换组
Apache DolphinScheduler源码分析(超详细)
随机推荐
Apache dolphin scheduler source code analysis (super detailed)
漏了监控:Zabbix对Eureka instance状态监控
Basic commands of MySQL
Blue Bridge Cup zero Foundation National Championship - day 20
[hot100] 739. Température quotidienne
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
接口自动化测试框架:Pytest+Allure+Excel
将ue4程序嵌入qt界面显示
【每日一题】729. 我的日程安排表 I
Development of entity developer database application
[daily question] 729 My schedule I
ROS learning_ Basics
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Bio model realizes multi person chat
UWA pipeline version 2.2.1 update instructions
Simple query cost estimation
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
Missing monitoring: ZABBIX monitors the status of Eureka instance
【Hot100】739. Daily temperature