Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

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;  // c : 1
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;  // c : 2
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);  // 'Green'
Copy the code

Any

Sometimes, we want to specify a type for variables whose type is not clear at programming time. These values may come from dynamic content, such as user input or third-party code libraries. In this case, we don’t want the type checker to check these values and just pass them through compile-time checks. We can then mark these variables with type any:

let notSure: any = 233;
notSure = "hello";  // OK
notSure = true;   // OK
Copy the code

Void

In a way, void is like the opposite of any; it means there is no type at all. When a function returns no value, you usually see the return type void:

function warnUser() :void {
    console.log("This is my warning message");
}
Copy the code

Declaring a void variable doesn’t do much good, because you can only assign undefined and null to it:

let unusable: void = undefined;
Copy the code

Undefined and Null

In TypeScript, undefined and null have their own types called undefined and NULL, respectively. Like void, their own types are not very useful:

let y: void = undefined;
y = null;
 
let z: undefined = null;
z = undefined;
Copy the code

By default null and undefined are subtypes of all types. This means that you can assign null and undefined to variables of type number

However, when you specify the – strictNullChecks flag, null and undefined can only be assigned to void and their respective checks

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

// 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() :never {
    return error("Something failed");
}
 
// A function that returns never must have an unreachable end
function infiniteLoop() :never {
    while (true) {}}Copy the code

Types of assertions

Sometimes you’ll find that you know more about a value than TypeScript does. Usually this happens when you clearly know that an entity has a more exact type than its existing type.

Type assertions are a way of telling the compiler, “Trust me, I know what I’m doing.” Type assertion is like conversion in other languages, but without special data checking and deconstruction. It has no run-time impact, only at compile time. TypeScript assumes that you, the programmer, have done the necessary checks.

Type assertions take two forms. One is an Angle bracket syntax

let someValue: any = "this is a string";
letstrLength: number = (<string>someValue).length; For anotherasGrammar:let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
Copy the code

Praise support, hand left lingering fragrance, with rongyan, move your rich little hands yo, thank you big guy can leave your footprints.

Past wonderful recommendation

Front-end common encryption methods

Canvas Pit Climbing Road

Don’t know SEO optimization? An article helps you understand how to do SEO optimization

Canvas Pit Road

Wechat small program development guide and optimization practice

Talk about mobile adaptation

Front-end performance optimization for actual combat

Talk about annoying regular expressions

Obtain file BLOB stream address to achieve the download function

Vue virtual DOM confused? This article will get you through the virtual DOM once and for all

Git Git

Easy to understand Git introduction

Git implements automatic push

Interview Recommendations

Front ten thousand literal classics – the foundation

Front swastika area – advanced section

More exciting details: personal homepage