当前位置:网站首页>TS initial use, TS type
TS initial use, TS type
2022-07-01 23:34:00 【qq_ forty-six million three hundred and two thousand two hundre】
ts Type statement
// Parameters a,b, The type of the return value of the function is declared as number type , If there is a non in the transmission parameter number Data of type , A red wavy line will be displayed
function sum(a: number, b: number): number {
return a + b
}
console.log(sum(1, 9)); //10
// Here a amount to const Defined ,a The value of is always 10
let a: 10;
//b The value of can only be 'male' perhaps 'female'
let b: 'male' | 'female'
//b The value of can only be boolean Type or string type
let c: boolean | string
//any Represents any type of , You can give d Assign any type of value ( It's equivalent to d Shut down the TS Type detection of )
let d: any
d = true
// Equate to let d
let s: string
s = d // d yes any type ,s Also became any type ( So try to avoid using any)
//unknown Indicates an unknown type , You can also give d Assign any type of value
let e: unknown
e = 'hellow'
let s: string
//s = e // Grammar detection failed , because s yes string type ,e yes unknown type , Their types are different , Direct assignment is not allowed
// If you just want to e The value is assigned to s, There are two solutions
Method 1 :
if(typeof e === "string") {
s = e
}
Method 2 :
// Use type assertion
s = e as string
perhaps :
s = <string>e
// void It is used to express empty , Take functions for example , It means that there is no return value
function fn(): void {
}
function fn(): void {
//return undefined
//return;
//return null
}
Once the function reports an error, it immediately terminates ,never Rarely used
// {} Used to specify the object type
let a: {
name: string}
// a There are and only name attribute , And name Value must be string type
a = {
name: ' Pig eight quit '}
let b: {
name: string, age?: number}
// b There must be name attribute , And name Value must be string type ;age not essential ;
b = {
name: ' Pig eight quit '}
perhaps
b = {
name: ' Pig eight quit ', age: 18}
let c: {
name: string, [propName: string]: number}
// b There must be name attribute , And name Value must be string type ; Number of remaining attributes , Attribute names are not required , But it must be number type
c = {
name: ' Pig eight quit ', haha: 12, xixi: 99}
// () => {} Used to specify the function object type
let d: (a: number, b: number)=>number
// function d Only two are received number Parameters of type , And the return value must be number type
d = function(n1: number, n2: number): number {
return n1 + n2
}
// [] Used to specify the array type
let a: number[]
Equate to
let a: Array<number>
a = [1,3,6,5,9]
//tuple Is a tuple (ts newly added ), Tuples represent fixed length arrays
let a: [string, number]
// Array a There are only two elements in it , The first is string type , The second is number type
a = [1, '2']
//enum Is the enumeration (ts newly added )
enum Gender{
male,
female
}
let a: {
name: string, gender: Gender}
a = {
name: ' Pig eight quit ', gender: Gender.male}
// & Indicates simultaneous
let a: {
name: string} & {
age: number}
Equate to
let a: {
name: string, age: number}
// object a There must be both name and age, And the type is the specified type
a = {
name: ' Pig eight quit ', age: 18}
// Alias for type
type myType = 1 | 2 | 3
let a: myType
let c: mType
a = 1, c = 3
// a、c The value of is 1 or 2 or 3, Will do
边栏推荐
- 神经网络物联网的未来趋势与发展
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
- TS初次使用、ts类型
- mysql:insert ignore、insert和replace区别
- 共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
- 【ES实战】ES上的安全性运行方式
- excel如何打开100万行以上的csv文件
- 2021 robocom world robot developer competition - semi finals of higher vocational group
- 物联网技术应用属于什么专业分类
- 物联网应用技术专业是属于什么类
猜你喜欢
What category does the Internet of things application technology major belong to
MT manager test skiing Adventure
2022 R1 fast opening pressure vessel operation test questions and answers
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
Practical application and extension of plain framework
第六章 数据流建模
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
Concepts of dictionary, hash table and array
深度学习 | 三个概念:Epoch, Batch, Iteration
Airserver latest win64 bit personal screen projection software
随机推荐
flutter Unable to load asset: assets/images/888. png
jpa手写sql,用自定义实体类接收
golang中的iota
Leetcode(34)——在排序数组中查找元素的第一个和最后一个位置
y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
ARP报文头部格式和请求流程
转行软件测试,知道这四点就够了!
2021 robocom world robot developer competition - preliminary competition of higher vocational group
VIM color the catalogue
JS - use of arguments
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
Matplotlib常用图表
dat. GUI
2021 RoboCom 世界机器人开发者大赛-本科组初赛
[applet] realize the left and right [sliding] list through the scroll view component
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
PostgreSQL notes (10) dynamically execute syntax parsing process
Linux foundation - centos7 offline installation of MySQL
Daily three questions 6.30