当前位置:网站首页>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;
边栏推荐
- 8VC Venture Cup 2017 - Elimination Round D. PolandBall and Polygon
- Oracle 条件、循环语句
- 数据中台:数据治理的建设思路以及落地经验
- Concurrent wait/notify description
- Qcom LCD commissioning
- OpenSSL client programming: SSL session failure caused by an obscure function
- Wedding studio portal applet based on wechat applet
- Linked list in JS (including leetcode examples) < continuous update ~>
- 【LeetCode】12、整数转罗马数字
- 8VC Venture Cup 2017 - Elimination Round D. PolandBall and Polygon
猜你喜欢

Oracle 常用基础函数

How to do a good job of dam safety monitoring

How high is the gold content of grade II cost engineer certificate? Just look at this

Voltage mode and current mode control of switching power supply

JS text box loses focus to modify width text and symbols

WordPress zibll sub theme 6.4.1 happy version is free of authorization

Application of Beidou No.3 short message terminal in dam safety monitoring scheme

独立站卖家都在用的五大电子邮件营销技巧,你知道吗?

When excel copies the contents of a row, the columns are separated by the tab "\t"

Interpretation of cloud native microservice technology trend
随机推荐
Yin Yang master page
Extjs library management system source code intelligent library management system source code
When excel copies the contents of a row, the columns are separated by the tab "\t"
Interpretation of cloud native microservice technology trend
Docker安装Mysql5.7并开启binlog
Yunda's cloud based business in Taiwan construction 𞓜 practical school
jsp连接Oracle实现登录注册
SlicePlane的Heading角度与Math.atan2(y,x)的对应转换关系
Oracle基础知识总结
数据中台:数据治理的建设思路以及落地经验
Dart学习——函数、类
msa.h:没有那个文件或目录
Docker installs mysql5.7 and starts binlog
独立站卖家都在用的五大电子邮件营销技巧,你知道吗?
中小型水库大坝安全自动监测系统解决方案
Programmer - Shepherd
[Linux] - using xshell to install MySQL on Linux and realize the deployment of webapp
DPDK 源码测试时性能下降问题
解决ValueError: Iterable over raw text documents expected, string object received.
2022 Western pastry (Advanced) test question simulation test platform operation