1. What is typescript?

Typescript is a superset of JavaScript that extends the syntax of JavaScript and can be compiled into JavaScript syntax.

2. Typescript’s features and benefits

JavaScript code works with TypeScript without modification, and TypeScript provides static type checking at compile time through type annotations. TypeScript handles existing JavaScript code and compiles only TypeScript code within it. TypeScript increases the readability and maintainability of your code, and makes your programming more efficient by finding errors at compile time.

3. The installation of the typescript

NPM install typescript -g for Windows and sudo NPM install typescript -g for MAC. A file compiled to javascript using TSC index.ts

4. Basic data types

Typescript and javascript declare variables differently:

let isDone: boolean = false; //typescript declaration let isDone=true; // in the js declaration, we will find the following: Boolean, the purpose of this is to restrict this variable to only Boolean data, not anything else, but we don't do that in javascript, so you're going to be true, I can change it to Number, A =true,a = "abcd", and "abcd" is printed out, but we do the same thing in ts. "Type 'string' is not assignable to Type 'Boolean'." Can only be changed to a BooleanCopy the code

Let name: string = “Bob “; let decLiteral: number = 6; The value can only be a string or a number, and can only be changed to the data of the corresponding type. Note that numeric literals can also be set to binary and octal literals. For example, let hexLiteral: number = 0xf00d; let binaryLiteral: number = 0b1010;

Both undefined and NULL have their own types called undefined and NULL respectively. They are not very useful by themselves. They are declared as strings and numbers. Let u: undefined = undefined; let n: null = null;

Note: By default null and undefined are subtypes of all types. For example, let num:number=null/undefined;

5. Array type

TypeScript works with array elements just like JavaScript does. There are two ways to define an array:

let list: number[] = [1, 2, 3]; — Element type +[]

let list: Array = [1, 2, 3]; — Use the Array generic, Array< element type >

The element type limits the Number of elements inside our array

We get an error when a string appears in an array of type Number, and we get an error when we run it

That means type “string” cannot be assigned to type “Number”

So how do we how do we make an array, or a variable, be able to be any value, or change to any value, or specify a type for a variable whose type is not clear?

Data of any type

When we don’t know what the data type of a variable is we can specify any, and any data can be assigned to any other type of data.If p1 is of type any, we assign the values to each of them. The printed results are 12, ABC, and true. This means that variables of type any can be assigned to other variables

Similarly, when we set the element type of an array to any, the elements in the array can be different types of elements

Void type data

The void type is like the opposite of any, which means there is no type. When a function does not return a value, you will usually see a return value of type void:

function warnUser(): void { console.log(“This is my warning message”); }

And declaring a void variable is useless because you can only assign undefined and null to it;

Type inference

If no Type is explicitly specified, TypeScript concludes a Type according to the rules of Type Inference.

let myFavoriteNumber = 'seven'; myFavoriteNumber = 7; In this code, the yFavoriteNumber variable does not specify a type. Type 'number' is not assignable to Type 'string'. In fact, let myFavoriteNumber: string = 'seven'. myFavoriteNumber = 7;Copy the code

TypeScript anticipates a type when there is no explicitly specified type. This is type inference.