TypeScript related issues and knowledge points

Q1: What is the difference between type and interface in TS?

A1: The similarities and differences are as follows.

1. Similarities:

(1) Both can define objects and functions.

interface:

 interface Person{
  name: string;
  age: number;
}
interface SetPerson {
  (name: string, age: number): void;
}
Copy the code

type:

type Person= {
  name: string;
  age: number
};
type SetPerson = (name: string, age: number)=> void;
Copy the code

(2) Both can be inherited. An object defined by interface is inherited with extends, and type is inherited with &. The two can inherit from each other using their own syntax, as mentioned earlier.

2. Differences:

(1) Interface can be declared merge, that is, declared multiple interfaces with the same name can be merged into a single, but type is not.

interface Pesron{ name: string; age: number; } interface Person{ sex: string; } /* Person interface {name: string; age: number; sex: string ; } * /Copy the code

(2) Type can declare aliases, union types, tuples, and other types of basic types, but interface cannot.

Void =null; Interface Person1{sayHi(); } interface Person2{ eat(); } type Person = Person1 | Person2; type ex = number | string; // Tuple arrays are usually of the same data type (arrays of type any[] can be different). If you store elements of different data types, you need to use tuples. type tuple=[1,'good']; Let tem = new Number(); // The type statement can be assigned with typeof. type B = typeof tem;Copy the code

(3) There are other complex operations, generics, etc.

Q2: What is the difference between interface and class? When are they used?

A2: Both interfaces and classes can define the data model. The difference between:…

Difference: Interface is only used to declare object types or methods, not implementation; Class is the declaration and implementation of a class.

  • Simple data model, directly for presentation, defined with interface;
  • More complex data models, with field property definitions and methods, require class. There are alsoconstructorConstructor.
  • Interfaces are only used for type checking at compile time, and classes are actually prototypes in javascript when compiled.

Interfaces can inherit from classes through extends, and classes can implement interfaces through implements. Here’s a good example to help you understand.

Q3: What do you know about generics in TS?

A3: case

1. Without generics, this function might look something like this:

function identity(arg: number): number {
    return arg;
}
Copy the code

Alternatively, we use the any type to define the function:

function identity(arg: any): any {
    return arg;
}
Copy the code

In the case of any, it could be anything. Therefore, we need a way to make the type of the return value the same as the type of the parameters passed in. In this case, we use T, the type variable, which is a special kind of variable that only represents the type and not the value.

function identity<T>(arg: T): T {
    return arg;
}
Copy the code

This allows you to track the type information used in the function.

2. There are two ways to use generics:

Here refer to the official document rewrite.

Let output = identity< string> (“myString”); // Type of output will be ‘string’; // Type of output will be ‘string’;

Let output = identity(“myString”); let output = identity(“myString”); let output = identity(“myString”); // type of output will be ‘string’

Special Statement: I am the same person as Aaqingying of CSDN.

The link to my blog is blog.csdn.net/aaqingying

This article is the original article, reproduced please indicate the source.