当前位置:网站首页>Typescript raw data type
Typescript raw data type
2022-06-24 15:16:00 【Emperor_ LawD】
data type
summary
- The type declaration is TS A very important feature
- Type declarations allow you to specify TS Medium variable ( Parameters 、 Shape parameter ) The type of
- After specifying the type , When assigning a value to a variable ,TS The compiler will automatically check whether the value conforms to the type declaration , If yes, the value is assigned , Otherwise, the report will be wrong
- In short , The type declaration sets the type... For the variable , So that variables can only store certain types of values
- TypeScript There are two types of data in
- Raw data type ( Basic data type )
- object type ( Complex data type )
- Common basic data types :number / string / boolean / undefined / null
- Automatic type determination
- TS Have automatic type judgment mechanism
- Simultaneous declaration and assignment of variables ,TS The compiler determines the type of variable
- So if you declare and assign variables at the same time , You can omit the type declaration
type
type | Example | describe |
---|---|---|
number | 1,-2,3.4 | Arbitrary number |
string | "hi",'hi', | Any string |
boolean | true、false | Boolean value true or false |
Literal | Its own | Limiting the value of a variable is changing the literal value |
any | * | Any type |
unknown | * | Type safe any |
void | Null value (undefined) | No value ( or undefined) |
never | No value | Cannot be any value |
object | (name: ' ROM. ') | Any of the js object |
array | 1, 2, 3 | Any of the js Array |
tuple | 4, 5 | Tuples ,TS New type , Fixed length array |
enum | enum(A, B) | enumeration ,TS New type in |
stay ES6 and ES10 New basic data types are introduced in , Namely
Symbol
andBigInt
Raw data type
Numeric type
Use keywords
number
Define the number type
let decimal: number = 6.1; // decimal let hex: number = 0xf00d; // Hexadecimal let binary: number = 0b1010; // Binary system let octal: number = 00744; // octal let big: bigint = 100n;
String type
Use keywords
string
Define string type
let color: string = 'bule'; color = 'red'; let fullName: string = 'Law D'; let age: number = 12; let sentence: string = `Hello, my name is ${fullName}.I am ${age} years old.` // among `` Used to define ES6 Template string in , Its effect is equal to let sentence: string = 'Hello, my name is' + fullName + '.I am' + age + 'years old.'
Boolean type
Use keywords
boolean
Define Boolean types
let isDone: boolean = false;
notes : Non strict mode number、string、boolean All values can be empty
Void type
stay TypeScript in , It can be used
void
Represents a function that does not have any return value
function hello(): void { alert("Hello TS"); } let unuseable: void = undefined; // Declared but unassigned variable values ( Value not found )
Null type
Indicates that the object is missing
let nu: null = null; // Declared and assigned ( Can find , The value is null)
Undefined type
Used to initialize a variable to an undefined value
let un: undefined = undefined;
- void and undefined The difference between
let un: undefined; let num: number = un; // Can be undefined A variable of type is assigned to number Variable of type let vo: void; let num: number = vo; // void Variables of type cannot be given to number Variable assignment of type
undefined
and null
Is a subtype of all types
边栏推荐
猜你喜欢
随机推荐
Database considerations
R language plot visualization: the visualization model creates a grid in the classification contour (contour) and meshgrid of the entire data space, in which the distance between each point is determi
【ansible问题处理】远程执行用户环境变量加载问题
左手代码,右手开源,开源路上的一份子
How to achieve long-term development of voice social source code?
Golang implements BigInteger large number calculation
Alibaba OSS object storage service
Explore cloud native databases and take a broad view of future technological development
update+catroot+c000021a+critical service failed+drivers+intelide+viaide+000000f
Qunhui synchronizes with alicloud OSS
Who do you want to open a stock account? Is online account opening safe?
同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩
FPGA based analog I ² C protocol system design (Part I)
Detailed explanation of redis data types
Laravel 8 realizes auth login
Esp32 series -- comparison of esp32 series
A simple and powerful developer toolkit box3 cc
R语言实战应用精讲50篇(二十三)-贝叶斯理论重要概念: 可信度Credibility, 模型Models, 和参数Parameters
Left hand code, right hand open source, part of the open source road
Bitmap of redis data structure