System learning TypeScript - basic type

preface

The basic types contained in TypeScript can be summarized as follows:

  • Boolean value
  • number
  • character string
  • array
  • tuple
  • enumeration
  • Arbitrary value
  • Null value
  • Null and undefined
  • Never
  • Object

Today, let's learn more about the meaning and representation of various types.

boolean → boolean

It has only two values -- true and false.

let isNew: boolean = true;

Number → number

The type of integer and floating-point number in TypeScript is number, which is the same as that in JavaScript. For example, the types of decimal, binary, octal and hexadecimal are number.

let decAge: number = 22;
let hexAge: number = 0x0016;
let binaryAge: number = 0b10110;
let octalAge: number = 0o026;
// The above variables use (number) ToString (10) is converted to decimal 22

You can use (number) ToString (base) converts a number to any base type.

String → string

Like JavaScript, the value of a string is wrapped in single or double quotation marks:

let myName: string = "Programming samadhi";
let myHomepage: string = `example.com/${myName}`;

array

There are two ways to define arrays in TypeScript.

The first is the element type followed by [], indicating an array composed of elements of this type:

let arr: number[] = [1, 2, 3, 4];
// If you add other elements to the array, an error will be reported

The second is to use array generics to define arrays:

let arr1: any[] = [1, "2", 3, "4"];
// This array can contain elements of any type

Tuple

Tuple type allows to represent an array with known number and type of elements, and the type of each element does not have to be the same.

let arr2:[number, string, number] = [1,"2",3];
// If it is written as [1,2,3], an error will be reported

The element strictly specifies the length of the array and the element type of each position, and it needs to be strictly corresponding during assignment, otherwise an error will be reported.

enumeration

enum type is a supplement to JavaScript standard data types. Like other languages such as C# and so on, using enumeration types can give friendly names to a set of values.

enum Color {Red, Green, Blue}
let c: Color = Color.Green;

By default, elements are numbered from 0. You can also manually specify the value of the member. For example, we change the above example to number from 1:

enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;

Alternatively, all are assigned manually:

enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;

One convenience provided by enumeration type is that you can get its name from the value of enumeration. For example, we know that the value is 2, but we are not sure which name it maps to in Color. We can find the corresponding name:

enum Color {Red = 1, Green, Blue}
let colorName: string = Color[2];

console.log(colorName);  // 'Green' is displayed because its value in the above code is 2

any value

Represents any type of value. If you don't want the type checker to check these values, just let them pass the check at the compilation stage., Then you can mark these variables with the any type:

let a: any = 12;
a = "12";
let list: any[] = [1, true, "free"];
list[1] = 100;

Null void

When a data does not have any type, it is usually marked with void, which is mostly used for the return value of the function.

function sayName(): void {
    console.log(`My name is Programming Samadhi.`);
}
let aNull: void = null;
let aNull1: void = undefined;

void type can only be assigned null or undefined.

null and undefined

TypeScript also has null and undefined types, which can only correspond to null and undefined values respectively.

let theNull: null = null;
let theUndefined: undefined = undefined;

These two types are basically of little use.

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 this option on.

Never

Never types represent the types of values that never exist. For example, never type is the return value type of function expression or arrow function expression that always throws an exception or has no return value at all; Variables can also be of type never when they are constrained by type protection that is never true.

Never type is a subtype of any type and can also be assigned to any type; However, no type is a subtype of never or can be assigned to never type (except never itself). Even any cannot be assigned to never.

Here are some functions that return never type:

// The function returning never must have an unreachable end point
function error(message: string): never {
    throw new Error(message);
}

// The inferred return value type is never
function fail() {
    return error("Something failed");
}

// The function returning never must have an unreachable end point
function infiniteLoop(): never {
    while (true) {
    }
}

object

object types contain all types except several basic types.

let obj: object = [1,2,3];
let obj1: object = {
    name: "Programming samadhi"
};
let func: object = ()=>{
    console.log("Programming samadhi");
}

extend

1. TypeScript will judge the default type according to the initial value of the variable you give.

let aNum = 12;
aNum = "number";
// Type 'string' is not assignable to type 'number'.

2. Type restrictions can be used for parameters and return values of function methods 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'.

summary

The above is the introduction of basic data types in TypeScript, which can be summarized as follows:

  • Add a colon after the variable (parenthesis of the function) and follow the expected type to limit the consistency of the type;
  • If there are no explicit restrictions on adding types, TypeScript automatically adds types based on the initial value.
~ End of this article, thank you for reading!

~

Learn interesting knowledge, make interesting friends and shape interesting souls!

Hello, I'm Programming samadhi Hermit Wang, my official account is " Programming samadhi "Welcome to pay attention and hope you can give us more advice!

Keywords: Javascript TypeScript

Added by stringfield on Thu, 24 Feb 2022 14:25:25 +0200