foreword
The basic types included in TypeScript are summarized as follows:
- Boolean value
- number
- string
- array
- tuple
- enumerate
- any value
- null
- Null and undefined
- Never
- Object
Today, let's take a closer look at the meaning and representation of each type.
boolean → boolean
It has only two values - true and false.
let isNew: boolean = true;
number → number
The integer and floating-point types in TypeScript are all numbers, which is the same as JavaScript, for example: decimal, binary, octal, and hexadecimal types are all numbers.
let decAge: number = 22;
let hexAge: number = 0x0016;
let binaryAge: number = 0b10110;
let octalAge: number = 0o026;
// 以上变量使用 (number).toString(10) 转换为十进制都为 22
Use (number).toString(base) to convert a number to any base type.
string → string
Like JavaScript, string values are enclosed in single or double quotes:
let myName: string = "编程三昧";
let myHomepage: string = `example.com/${myName}`;
array
There are two ways to define arrays in TypeScript.
The first is the element type followed []
, indicating an array of elements of this type:
let arr: number[] = [1, 2, 3, 4];
// 如果在数组中加入其他元素会报错
The second is to define an array using array generics:
let arr1: any[] = [1, "2", 3, "4"];
// 这个数组中可以假如任意类型的元素
Tuple Tuple
The tuple type allows to represent an array with a known number and type of elements, and the elements do not have to be of the same type.
let arr2:[number, string, number] = [1,"2",3];
// 若果写成 [1,2,3] 会报错
The elements strictly specify the length of the array and the element type of each position, and they need to correspond strictly when assigning, otherwise an error will be reported.
enumerate
enum
type is an addition to the JavaScript standard data types. Like other languages such as C#, using enumeration types allows you to give a set of values a friendly name.
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
By default, elements are numbered starting at 0
. You can also manually specify member values. For example, let's change the above example to start numbering from 1
:
enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;
Or, all with manual assignment:
enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;
One of the conveniences provided by enumeration types is that you can get the name of the enumeration from its value. For example, we know the value is 2, but are not sure which name in Color it maps to, we can look up the corresponding name:
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];
console.log(colorName); // 显示'Green'因为上面代码里它的值是2
any value any
Represents a value of any type. If you don't want the type checker to check these values and just let them pass the compile-time check. , then you can use the any
type to mark these variables:
let a: any = 12;
a = "12";
let list: any[] = [1, true, "free"];
list[1] = 100;
void void
When a data does not have any type, it is usually marked with void, which is mostly used for function return values.
function sayName(): void {
console.log(`My name is 编程三昧。`);
}
let aNull: void = null;
let aNull1: void = undefined;
void types can only be assigned null or undefined.
null and undefined
There are also two types of null and undefined in TypeScript, which can only correspond to null and undefined values respectively.
let theNull: null = null;
let theUndefined: undefined = undefined;
These two types are basically useless.
Whether null and undefined can be assigned to variables of type number depends on whether the "--strictNullChecks" option in the compilation configuration file is turned off. It is generally recommended to turn on this option.
Never
never
type represents the type of values that never exist. For example, the type never
is the return type of those function expressions or arrow function expressions that always throw an exception or never return a value at all; variables may also be of type never
when they are never true when bound by protection.
never
type is a subtype of, and assignable to, any type; however, type is not a of never
or assignable to the never
type (except for never
itself). Even any
cannot be assigned to never
.
Here are some functions that return the type never
:
// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
throw new Error(message);
}
// 推断的返回值类型为never
function fail() {
return error("Something failed");
}
// 返回never的函数必须存在无法达到的终点
function infiniteLoop(): never {
while (true) {
}
}
object
The object type contains all but a few primitive types.
let obj: object = [1,2,3];
let obj1: object = {
name: "编程三昧"
};
let func: object = ()=>{
console.log("编程三昧");
}
expand
1. TypeScript will determine the default type based on the initial value you give to the variable.
let aNum = 12;
aNum = "number";
// Type 'string' is not assignable to type 'number'.
2. The parameters and return values of function methods can use type restrictions to ensure the correctness of parameters and return values.
function sum(a: number, b:number): number {
return a + b;
}
sum(1, "3");
// Argument of type 'string' is not assignable to parameter of type 'number'.
Summarize
The above is an introduction to the basic data types in TypeScript, summed up as follows:
- Add a colon after the variable (the parentheses of the function), followed by the expected type, to limit the consistency of the type;
- If there is no explicit type restriction added, TypeScript will automatically add the type based on the initial value.
~
~ This article is over, thanks for reading!
~
Learn interesting knowledge, meet interesting friends, and shape interesting souls!
Hello everyone, I'm 1621e03c4a54f1 The King of , the author of 〖 Programming Samadhi 〗, my official account is " Programming Samadhi ", welcome to pay attention, I hope you can give me more advice!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。