Getting started with TS (what are the types of TS? How to use it?)

Introduction to TS

TS(TypeScript)It is an open source programming language developed by Microsoft. It is JavaScripta superset of JavaScript, which can add static type checking and object-oriented programming features to JavaScript. TSType checking can be done at compile time, improving code readability, maintainability, and reliability, while also supporting the ECMAScriptlatest features of . TSIt can run on any platform that supports JavaScript, and can be JavaScriptseamlessly integrated with existing code. TS also has good support for development tools, such as Visual Studio Code, WebStormand other commonly used IDEs support TSthe development of .

insert image description here

Commonly used types in TypeScript include the following:

Any type ( any): Indicates any type, which can be assigned any value.

Number type ( number): Indicates a number type, including integers and floating point numbers.

String type ( string): Indicates a string type, including single quotes and double quotes.

Boolean type ( boolean): Indicates Boolean type, including true and false.

Array type ( array): Indicates the array type, which can contain elements of any type.

Tuple type ( tuple): Indicates the tuple type, which can contain a fixed number and type of elements.

Enumeration type ( enum): Indicates an enumeration type, which can define a set of named constants.

nulland undefinedType: Represents null and undefinedtype, which represent null and undefined values, respectively.

voidType: Indicates that there is no type, usually used for function return values.

objectType: Indicates non-primitive types, that is, types other than number, string, boolean, nulland undefined, including objects, arrays, functions, etc.

neverType: A type representing a value that never exists, usually used for the return value of a function.

insert image description here

Types in TypeScript can be used to define variables, functions, classes, interfaces, etc. Here are some common usages:

Define the type of the variable

typescript
Copy code
let age: number = 18;
let name: string = "Tom";
let isStudent: boolean = true;

Define the parameter types and return type of the function

typescript
Copy code
function add(x: number, y: number): number {
  return x + y;
}

Defines the types of arrays and tuples

typescript
Copy code
let nums: number[] = [1, 2, 3];
let info: [string, number] = ["Tom", 18];

Define the type of object

typescript
Copy code
interface Person {
  name: string;
  age: number;
}
let tom: Person = {
  name: "Tom",
  age: 18
};

define the type of the class

typescript
Copy code
class Person {
  name: string;
  age: number;
  
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}
let tom: Person = new Person("Tom", 18);

These type usages are just TypeScriptthe tip of the iceberg of the types in , and there are many advanced usages, such as joint types, intersection types, type aliases, generics, etc., which can be flexibly used according to actual needs.

insert image description here

The main differences between JavaScript (JS for short) and TypeScript (TS for short) are as follows:

Type system : JavaScriptIt is a weakly typed language, and the type of variables can only be determined at runtime. It TypeScriptis a strongly typed language, the type of the variable has been determined at compile time, and stricter type checking can be performed.

Static type checking : TypeScriptStatic type checking is provided, which can detect type errors at compile time and avoid type errors at runtime. However JavaScript, type errors can only be found at runtime, which can easily lead to program crashes or unexpected behavior.

ES6+ feature support : TypeScriptcan support the latest ECMAScriptstandards, including ES6, ES7, ES8etc., and JavaScriptthe degree of support depends on the version of the browser or Node.js.

Development tool support : TypeScriptThe support for development tools is more friendly, for example Visual Studio Code, WebStormand other commonly used developments that IDEare supported TScan provide more intelligent prompts and code completion.

Difficulty of learning : TypeScriptCompared with JavaScriptthe difficulty of learning, it needs to master more knowledge about grammar and type system. However, TypeScriptthe type system can improve the readability, maintainability and reliability of the code, which is very beneficial for large-scale project development.
In addition, TypeScriptit can be easily integrated into JavaScriptthe project, and the code can be gradually JavaScriptconverted into TypeScriptthe code, which avoids the tedious refactoring and ensures the compatibility of the code.

Guess you like

Origin blog.csdn.net/weixin_52703987/article/details/130866129