“This is my 38th day of participating in the First Challenge 2022. For details: First Challenge 2022”
preface
The basic types that TypeScript contains are summarized as follows:
- Boolean value
- digital
- string
- An array of
- tuples
- The enumeration
- Any value
- A null value
- Null, and undefined
- Never
- Object
Today, we will take a closer look at what each type represents and how it is represented.
Boolean → Boolean
It has only two values — true and false.
let isNew: boolean = true;
Copy the code
The Numbers – the number
In TypeScript, both the integer and floating point types are number, as in JavaScript, for example, the decimal, binary, octal, and hexadecimal types are number.
let decAge: number = 22;
let hexAge: number = 0x0016;
let binaryAge: number = 0b10110;
let octalAge: number = 0o026;
// The above variables are converted to decimal 22 using (number).tostring (10)
Copy the code
A number can be converted to any base type using (number).tostring (base).
String → string
Like JavaScript, string values are wrapped in either single or double quotation marks:
let myName: string = Programming Samadhi;
let myHomepage: string = `example.com/${myName}`;
Copy the code
An array of
There are two ways to define arrays in TypeScript.
The first is an array of elements of that type followed by [] :
let arr: number[] = [1.2.3.4];
// An error is reported if other elements are added to the array
Copy the code
The second way is to define arrays using array generics:
let arr1: any[] = [1."2".3."4"];
// This array can contain any type of element
Copy the code
Tuples Tuple
The tuple type allows you to represent an array with a known number and type of elements that need not be of the same type.
let arr2:[number.string.number] = [1."2".3];
// error if written as [1,2,3]
Copy the code
Elements are strictly specified for the length of the array and the type of the element at each position, and assignments must be strictly corresponding to each other, otherwise an error will be reported.
The enumeration
Enum types complement the JavaScript standard data types. As in other languages such as C#, enumeration types can be used to give friendly names to a set of values.
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
Copy the code
By default, elements are numbered from 0. You can also manually specify the values of the members. For example, let’s change the above example to number from 1:
enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;
Copy the code
Alternatively, all manual assignments are used:
enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;
Copy the code
One of the conveniences provided by an enumerated type is that you can refer to its name by enumeration. For example, if we know that the value is 2, but are not sure which name in Color it maps to, we can look for the corresponding name:
enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];
console.log(colorName); // Display 'Green' because its value is 2 in the code above
Copy the code
Any arbitrary value
Represents any type of value. If you don’t want the type checker to check these values you simply pass them through compile-time checks. , then we can mark these variables with type any:
let a: any = 12;
a = "12";
Copy the code
let list: any[] = [1.true."free"];
list[1] = 100;
Copy the code
Null void
When a data has no type, void is usually used to mark the value returned by a function.
function sayName() :void {
console.log('My name is programming Samadhi. `);
}
let aNull: void = null;
let aNull1: void = undefined;
Copy the code
Void can only be assigned to null or undefined.
Null, and undefined
TypeScript also has null and undefined, which correspond only to null and undefined, respectively.
let theNull: null = null;
let theUndefined: undefined = undefined;
Copy the code
These two types are basically useless.
Whether null and undefined can be assigned to variables of type number depends on whether the “–strictNullChecks” option is turned off in the compilation configuration file, which is generally recommended.
Never
The never type represents the types of values that never exist. For example, the never type is the return type of function expressions or arrow function expressions that always throw an exception or have no return value at all; Variables can also be of type never, when they are bound by type protection that is never true.
The never type is a subtype of any type and can be assigned to any type; However, no type is a subtype of never or can be assigned to a type of never (except never itself). Even any cannot be assigned to never.
Here are some functions that return type never:
// A function that returns never must have an unreachable end
function error(message: string) :never {
throw new Error(message);
}
// The inferred return value type is never
function fail() {
return error("Something failed");
}
// A function that returns never must have an unreachable end
function infiniteLoop() :never {
while (true) {}}Copy the code
object
The object type contains all but a few basic types.
let obj: object = [1.2.3];
let obj1: object = {
name: Programming Samadhi
};
let func: object = () = >{
console.log(Programming Samadhi);
}
Copy the code
extension
1. TypeScript determines the default type of a variable based on the initial value you give it.
let aNum = 12;
aNum = "number";
// Type 'string' is not assignable to type 'number'.
Copy the code
2. You can use type restrictions on the parameters and return values of a function method to ensure the correctness of the 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'.
Copy the code
conclusion
That’s the basic data types in TypeScript, summed up as follows:
- To limit type consistency, add a colon after a variable (function parentheses) followed by the desired type.
- If there are no explicit restrictions on adding types, TypeScript automatically adds types based on initial values.
~
Thanks for reading!
~
Learn interesting knowledge, meet interesting friends, shape interesting soul!
Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!