当前位置:网站首页>Typescript base type
Typescript base type
2022-06-28 05:30:00 【Ahchoo ahchoo-】
TypeScript Support JavaScript Basic data types for , Practical enumeration types are also provided
- boolean,string,null,undefined,number
/** * value type */
let num: number = 1
console.log(num);
console.log('==============');
/** * character string */
let str: string = ' character string '
console.log(str);
console.log('==============');
/** * Boolean */
let bool: boolean = true
console.log(bool);
console.log('==============');
// summary : ts The variable in starts by defining what type , Only this type can be assigned , Assigning other types of values is not allowed
/** * null and undefined */
let nul: null = null
let und:undefined = undefined
console.log(nul,und);
//undefined and null Can be used as a subtype of other types , That is to say undefined and null Assign values to other types of variables
let num2: number = 2
num2 = null
console.log('num2',num2);
console.log('==============');
- An array type
/** * An array type */
// Array definition 1: let Variable name : data type [] = [ value 1, value 2,...]
let arr1: number[] = [10,20,30]
console.log(arr1);
// Array definition 2, Array generics : let Variable name :Array< data type > = [ value 1, value 2,...]
let arr2: Array<number> = [100,200,300]
let srtArr: Array<string> = ['1','2','3']
console.log(arr2);
console.log(srtArr);
console.log('==============');
// Be careful : After the array definition , The value type must be the same as the type defined when defining the array
- A tuple type Tuple
Tuple types allow you to represent an array of known element data types and quantities , Each element type does not have to be the same
/** * A tuple type */
let arr3: [boolean,string,number] = [true,' Britney Spears ',100.12345]
console.log(arr3);
console.log(arr3[1].split(''));
console.log(arr3[2].toFixed(2));
// Be careful : When using tuple types , Type of data 、 The position and number are related to the type of element when the tuple is defined 、 The position corresponds to the number
- enumeration
There is a group of data commonly used , And the number is fixed , It can be defined as enumeration type . The enumeration type is ts Chinese vs js The basic data types of , Enumerations can be used to give friendly names to a set of data
// Each data value in an enumeration can be called an element , Every element is numbered , The numbers are incremented .
// If you do not assign a value to the element in the enumeration , Then the value is number , If assigned, it is the assigned value , And the number of other elements will also become the value incremented after the assignment
enum Color {
red,
blue=10,
green
}
let red: Color = Color.red
console.log(red);
console.log(Color.red, Color.blue, Color.green);
console.log(Color);
// You can get the numerical value of the element in the enumeration type by numbering
console.log(Color[10]);
// The elements in the enumeration can be Chinese numeric values , But it is not recommended.
- any type
any Type defines a value of any type
let temp: any = 100
temp = 'any type '
console.log(temp);
// When an array to store multiple types of uncertainty , When the number of data is uncertain , You can also use any Array
let anyArr: any[] = [100, '1', {
name: ' Xiaohong ', age: 1}, true]
console.log(anyArr);
// summary :any The advantage of type is that it can store data of uncertain type and number , But using the wrong syntax will not report an error , for example :console.log(anyArr[0].splice(''));
- void type
void The type and any The type is the opposite , It means there is no type , When the function has no return value , The return value type is void
// Declare a function that has no return value
function showMsg(): void{
console.log(' This is a function with no return value ');
// return
// return undefined
return null
}
console.log(showMsg());
// Definition void Variable of type , You can accept a undefined or null Value
let vd: void = undefined
console.log(vd);
- object type , Non primitive type
// Define a function , Parameter is object type , The return value is also object type
function getObj(obj: object): object{
console.log(obj);
return {
name: ' Daming ',
age: 27
}
}
// console.log(getObj({name: ' The Ming ',age: 25}));
// console.log(getObj(new String('123')));
console.log(getObj(String));
- Union type and type assertion
The union type indicates that the value can be one of multiple types
Types of assertions : Tell the editor , Know what type of value this variable is , And I know what I'm doing
// 1. Define a function , Receive one Number Type or String Parameters of type , Get the value in string form
// function getStr(str:number|string): string{
// return str.toString()
// }
//2. Define a function , Receive one Number Type or String Parameters of type , Get the length of the string value
/** * Types of assertions */
// Grammatical way 1: < type > Variable name
// Grammatical way 2: Variable name as type
function getStrLen(str: number | string): number{
if((<string>str).length) {
return (str as string).length
}else {
return str.toString().length
}
}
console.log(getStrLen(1234));
console.log(getStrLen('123456'));
- Type inference
ts It will infer a type when there is no explicit variable specified in the definition of the variable
//1. When defining a variable, it is assigned , Infer the type of assignment
let text = ' Purring '
//2. Variables are defined without assignment , Infer as any type
let x;
边栏推荐
- Online yaml to JSON tool
- How does guotaijun charge for safe varieties? Let's talk about the futures account opening process
- Gorm transaction experience
- 解决ValueError: Iterable over raw text documents expected, string object received.
- Linked list in JS (including leetcode examples) < continuous update ~>
- 如何在您的Shopify商店中添加实时聊天功能?
- msa. h: There is no such file or directory
- [skywalking] learn distributed link tracking skywalking at one go
- Yin Yang master page
- Dart学习——函数、类
猜你喜欢

Dart学习——函数、类

Biovendor sRAGE protein solution

如何在您的Shopify商店中添加实时聊天功能?

JS中的链表(含leetcode例题)<持续更新~>

What does mysql---where 1=1 mean

Docker installs mysql5.7 and starts binlog

Wedding studio portal applet based on wechat applet

线条动画

WordPress zibll sub theme 6.4.1 happy version is free of authorization

二级造价工程师证书含金量到底有多高?看这些就知道了
随机推荐
Extjs library management system source code intelligent library management system source code
Metartc5.0 API programming guide (I)
MySQL 45讲 | 05 深入浅出索引(下)
When using the MessageBox of class toplevel, a problem pops up in the window.
刘海屏手机在部分页面通过[[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom得到底部安全区高度为0问题
msa. h: There is no such file or directory
If a programmer goes to prison, will he be assigned to write code?
指定默认参数值 仍报错:error: the following arguments are required:
mysql 导出查询结果成 excel 文件
Informatics Orsay all in one 1360: strange lift
CSCI GA scheduling design
? How to write the position to output true
Unity delegate
How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years
Detailed usage configuration of the shutter textbutton, overview of the shutter buttonstyle style and Practice
关系数据库与文档数据库对比
Gorm transaction experience
Dart learning - functions, classes
2022 Western pastry (Advanced) test question simulation test platform operation
Solution of dam safety automatic monitoring system for medium and small reservoirs