当前位置:网站首页>Basic data type and structure data type of TS
Basic data type and structure data type of TS
2022-06-21 18:27:00 【CupidoZ】
TS Basic data type and structure data type of
// js Primitive types
let num:number = 5000;
let str:string = ' Hello , I'm a string ';
let bool : boolean = false;
let nul = null;
let undef;
let sym : symbol =Symbol('tag_info');
let obj_stu = {
id:'10086',
name:'cupido',
hobby:'swim'
}
let nums:number[] = [1,2,3,5,8,7]
let strs:Array<string> = ['a','ccc','sad','affad','123486']
// ts New inspection type
// Joint type
// Yes () Represents a mixed array
let arr : (number|string|boolean)[] = ['sad',12345,'haha',89898.2,'aa',false,true];
// No, () To express or relate to Can only be one of the specified types
let arr_or : number|string|boolean[] = [false,true];
// Type the alias
// Customize the data structure of the interface type Avoid declaring types repeatedly when reusing
type Mix_num_str = (number|string)[];
let arr_mix : Mix_num_str = [45654," ha-ha ",'asdafbjh',45564688,2.2,'45']
let arr_mixdemo:Mix_num_str=[11111,22222,'haha']
// Function type
function plus(a:number, b:number):number{
return a+b;
}
console.log(plus(1,2));
const add = (a:number,b:number):number=>{
return a+b;
}
console.log(add(1,2));
const add_obj :(a:number,b:number)=>number=(a,b)=>{
return a+b;};
// Empty type
function none(a:number, b:number):void{
console.log(' Functions with no return value ')
}
// Optional parameters
// Similar to variable parameters You can pass it on You can also pass any number of specified parameter types
function Slice(start?:number,end?:number):number{
return start+end;
}
Slice()
Slice(1)
Slice(1,2)
// Object type qualification
// Type declaration
var Person1 :{
id:number;name:string;gender:boolean;SayHi(name:string):void}={
id: 1001,
name: "cupido",
gender: false,
SayHi: function (name): void {
throw new Error("Function not implemented.");
}
}
// Optional attribute ?
var Person2 :{
id:number;name:string;gender?:boolean;SayHi(name:string):void}={
id: 1001,
name: "cupido",
SayHi: function (name): void {
throw new Error("Function not implemented.");
}
}
// Interface type
interface IPerson{
id:number;name:string;gender?:boolean;SayHi(name:string):void}
let person_obj : IPerson = {
id: 0,
name: "Lulu",
// Optional attribute
gender:true,
SayHi: function (name: string): void {
throw new Error("Function not implemented.");
}
}
// Interface inheritance
interface Point2D{
x:number;y:number}
interface Point3D extends Point2D{
z:number}
let point1:Point3D={
z: 0,
x: 0,
y: 0
}
// A tuple type tuple
// You can specify the element type and the number of parameters to check
let location_obj:[number,number]=[25.65,66.33]
// Type inference
let name1;
// name1=22 No error is reported at this time
let age = 18;
// age = 'a' When the variable type is not specified Type inference specifies the corresponding data type of variable declaration after variable initialization Change the variable type again and an error is reported
// Types of assertions
// Assertion keyword not used as when alink Parent class of HtmlElement There is no href attribute When we know alink The type of is a subclass HTMLAnchorElement when
// Can be done as Type designation So you can get subtypes HTMLAnchorElement Under the href attribute
// You can view the check information of the console for the parent class attribute a Labeled _protoType_ The type of
const alink = document.getElementById('alink') as HTMLAnchorElement
alink.href=='http://www......'
// Literal type
// str02 The type is 'cupido' Because it is a constant enumeration type So there will be unique types So strings can also appear as types But the values can only be the same
// This type of data can be understood as an enumeration
let str01 = 'cupido';
const str02 = 'cupido';
let str03:'cupido'='cupido';
function ChangeDirection1(derection:'up'|"down"|'left'|'right'){
console.log(' The direction of my movement is :'+derection)
}
// The corresponding constant is specified when calling
ChangeDirection1('up')
// Enumeration type
// If the enumeration member in the enumeration is not initialized There will be a default value and the default value will increase automatically from 0----1,1++.....
enum Derections{
UP = 'up',
DOWN='down',
LEFT='left',
RIGHT='right'
}
function ChangeDirection2(derection:Derections){
console.log(' The direction of my movement is :'+Derections)
}
// The corresponding constant is specified when calling
ChangeDirection2(Derections.UP)
// any type
// Use any When it comes to type Object will lose its type checking mechanism You can modify the behavior at will
let num_obj:any = 'haha';
num_obj=123456789;
let any_OBJ = {
id:123456,
maker:'xiaomi',
person:Person1,
date:new Date()
}
// typeof()
console.log(typeof(num_obj))
// It can be used for parameter type checking
function sayHI(name:typeof Person1): void {
throw new Error("Function not implemented.");
}
边栏推荐
- 新一代稳定性测试利器Fastbot
- 缓存型数据库Redis的配置与优化
- EtherCAT object dictionary analysis
- 力扣295. 数据流的中位数
- Can I use line as a product term in the cable industry? Generally not used
- PHP连接Mysql8.0报错:Illuminate\Database\QueryException
- Window常用快捷键
- Move Protocol Beta测试版稳定,临时决定奖池规模再扩大
- 电缆行业产品词可以用line吗?一般不用
- Stack awareness - stack overflow instance (ret2libc)
猜你喜欢

字節跳動提出輕量級高效新型網絡MoCoViT,在分類、檢測等CV任務上性能優於GhostNet、MobileNetV3!

2022年R1快开门式压力容器操作操作证考试题库模拟考试平台操作

Tcpserver enable multithreading

2022 high altitude installation, maintenance and removal work license question bank and simulated examination

小程序的宿主环境、组件、API、协同工作和发布

基本文件操作

Redis配置与优化

字节跳动提出轻量级高效新型网络MoCoViT,在分类、检测等CV任务上性能优于GhostNet、MobileNetV3!

数字藏品系统开发,NFT艺术品交易平台搭建

2022年高处安装、维护、拆除上岗证题库及模拟考试
随机推荐
剑指 Offer 28. 对称的二叉树
TypeScript对象类型
JZ59.按之字型顺序打印二叉树
基于AM4377的EtherCAT主站控制stm32从站
EtherCAT igh master station controls three Delta asdaa2 servo rotating circles
[technical management] assembly number and sword team
Node模块管理描述文件
Vue. js+Node. JS full stack development tutorial: connecting to MySQL
快速失败和安全失败的区别
Stm32f1 and stm32subeide programming example - linear Hall effect sensor driver
Stack cognition -- basic use of reverse IDA tools
科普大佬说 | 如何打造自己的AI创造力?
PHP输出函数
POSIX信号量
电缆行业产品词可以用line吗?一般不用
会议聊天室--开发文档
Differences between WCDMA and LTE
论文解读(USIB)《Towards Explanation for Unsupervised Graph-Level Representation Learning》
力扣239. 滑动窗口最大值
On two years' study experience in University