当前位置:网站首页>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 .
边栏推荐
- LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)
- Huawei 2023 starts to warm up in advance! Zuo Shen's program code interview guide comes in handy
- LeetCode - 359 日志速率限制器 (设计)
- Is there only one lib under JDBC in Seata?
- Leetcode - 232 realize queue with stack (design double stack to realize queue)
- MySQL-自增锁
- 开发者如何为React Native选择合适的数据库
- [server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage
- Recommended collection, which is probably the most comprehensive coding method summary of category type features
- Sword finger offer | number of 1 in binary
猜你喜欢

【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源

一文入门Redis

Beyond Compare 4 实现class文件对比【最新】

SVD singular value decomposition derivation and application and signal recovery

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

CVPR 2022 | in depth study of batch normalized estimation offset in network

Leetcode - 677 key value mapping (Design)*

Pytoch learning notes - Teacher Liu Er RNN advanced chapter - code comments and results

Pytoch learning notes -- Summary of common functions 3

Wavelet transform --dwt2 and wavedec2
随机推荐
MySQL隐式锁
Leetcode - 225 implements stack with queue
Redis分布式锁,没它真不行
Leetcode - 677 key value mapping (Design)*
Activity review | July 6 Anyuan AI X machine heart series lecture No. 2 | MIT professor Max tegmark shares "symbiotic evolution of human and AI"
30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)
Beyond compare 4 realizes class file comparison [latest]
没错,请求DNS服务器还可以使用UDP协议
LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)
Gary marcus: learning a language is more difficult than you think
I want to ask whether the variable configuration function can only be used in SQL mode
Componentization and modularization
Zhaoqi Kechuang high-level innovation and Entrepreneurship Talent Service Platform at home and abroad, mass entrepreneurship and innovation achievement transformation platform
乐观锁悲观锁适用场景
MySQL tutorial 66 data table query statement
推荐系统-协同过滤在Spark中的实现
The difference between VaR, let and Const
Mysql读写锁
Wechat applet
[Shakespeare: keep the fun of being a man]