当前位置:网站首页>Typescript learning 1 - data types
Typescript learning 1 - data types
2022-07-25 16:00:00 【Gai Yuexi's boyfriend outside the circle (kuaixi)】
explain
This article learns from the article of the great God of gold digging https://juejin.cn/post/7003171767560716302, The original author is the great rabbit God .
JavaScript Built in type
// number
let n:number=1;
// string
let s:string="str";
// boolean
let b:boolean=true;
// undefined
let u:undefined=undefined;
// null
let ne:null=null;
// object
let obj:object={};
// bigint
let bi:bigint=10n;
// symbol
let sy:symbol=Symbol("mySymbol");Array
Two definitions :
// Define the way 1
let arr1:string[]={' Hello ','hello'};
// Define the way 2
let arr2:Array<string>={' Hello ','hello'};Tuple
// Tuples can define arrays of different element types
let t:[string,number,boolean]=['hello',522,false];Tuples specify the length and element type of the array , An error will be reported if the type or length does not match . For arrays of uncertain type and length , It can be used any[] To define .
undefined and null
By default ,null and undefined Is a subtype of all types . in other words , You can put undefined and null Assign to variables of any type .
let m:number=2;
m=undefined;void
void Means there is no type of . Declare the variable as void It doesn't make sense , It will only be declared as void.
Be careful , Functions that do not return a value will get undefined, But it still needs to be declared void, Otherwise, an error will be reported .
unknown and any
any Will skip the check of the value by the type checker , Any value can be assigned to any type .
let a: any = 123;
a = '456';
a = false;unknown Can achieve the same effect .
let a: unknown = 123;
a = '456';
a = false; The biggest difference is , Any type of value can be assigned to any, meanwhile any The value of type can also be assigned to any type ; But for the unknown , Any type of value can be assigned to it , But it can only be assigned to unknown and any.
let a: unknown = 123;
let b: any = 123;
let c: number = 123;
// c = a;// Report errors
// a = c;// Sure
// b = c;// Sure
c = b;// Sure In the face of unknown Type before performing the operation , It must be narrowed down .
let obj: unknown = {
x: 1,
}
// console.log(obj.getX());// Report errors
// 1、 Use typeof
if (typeof obj === 'object' && obj !== null) {
console.log(obj.toString());
}
// 2、 Types of assertions
console.log((obj as object).toString())never
never Represents a type of value that never exists , There are two situations : Exception thrown during function execution , So there will never be a return value ; Function body is an endless loop , There will never be a return value .
// Throw an exception
function tErr(): never {
throw new Error('error');
}
// Dead cycle
function noRes(): never {
while (true);
}never Combined with other types , There will be no never.
// type t = string | number
type t = string | number | neverTypes of assertions
Similar to type conversion .
let str: any = "hello";
// 1、 Parenthesis grammar
let l: number = (<string>str).length;
// 2、as grammar
let l2: number = (str as string).length;type inference
When the variable type is not specified ,TS It will automatically infer the specific type .
let s = 'hello';
// s = 123;// Report errors , Because above s To be inferred as string type
let temp;
temp = 'hello';
temp = 1;// Sure , Because on the top temp To be inferred as any type Joint type
The type value is one of many types .
let a: string | number = 'hello';
a = 123;Cross type
Combine multiple types into one type , Add the existing multiple types together to form a type , It contains all the types of features you need .
interface IA {
name: string
age: number
}
interface IB {
name: string
id: number
}
// Need to include IA and IB All attributes of
let a: IA & IB = {
name: 'Jack',
age: 11,
id: 1
}a both IA type , It's also IB type .
边栏推荐
- 记得那两句话
- Introduction to redis
- MySQL 悲观锁
- SVD singular value decomposition derivation and application and signal recovery
- Reasons for data format conversion when matlab reads the displayed image
- Dpdk packet receiving and sending problem case: non packet receiving problem location triggered by mismatched packet sending and receiving function
- 2600 pages in total! Another divine interview manual is available~
- Pytoch learning notes -- Summary of common functions 2
- 报表工具的二次革命
- 墨天轮高分技术文档分享——数据库安全篇(共48个)
猜你喜欢

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

今天睡眠质量记录84分

通用测试用例写作规范

"Digital security" alert NFT's seven Scams

不愧是阿里内部“千亿级并发系统架构设计笔记”面面俱到,太全了

Leetcode - 232 realize queue with stack (design double stack to realize queue)

CVPR 2022 | 网络中批处理归一化估计偏移的深入研究

Pytoch learning notes -- Summary of common functions of pytoch 1

华为2023届提前批预热开始!左 神的程序代码面试指南终派上用场

I interviewed 8 companies and got 5 offers in a week. Share my experience
随机推荐
Matlab simulation of BPSK modulation system (1)
Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel
The difference between VaR, let and Const
# JWT 图解
MySQL tutorial 65 data in MySQL operation table
物理防火墙是什么?有什么作用?
leetcode:6127. 优质数对的数目【位运算找规律 + 两数之和大于等于k + 二分】
ML - Speech - traditional speech model
MySQL教程67-使用DISTINCT过滤重复数据
MySQL 元数据锁(MDL)
共2600页!又一份神级的面试手册面世~
Why is preparestatement better and safer?
"Digital security" alert NFT's seven Scams
Ml image depth learning and convolution neural network
MySQL tutorial 68-as setting alias
兆骑科创海内外高层次创新创业人才服务平台,双创成果转化平台
mysql 表读锁
华为2023届提前批预热开始!左 神的程序代码面试指南终派上用场
MySQL - Summary of common SQL statements
Dpdk packet receiving and sending problem case: non packet receiving problem location triggered by mismatched packet sending and receiving function