This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

If you feel helpful, please click 👍 to encourage you

Use type declarations to describe the type of an object

type myType = {
  name: string;
  age: number;
};
const person1: myType = {
  name: 'hzw'.age: 18};Copy the code
  • Interfaces are used to define a class structure that can be used as a type declaration
  • Interfaces are used to define which properties and methods should be included in a class
  interface myObiect {
    name: string;
    age: number;
  }
  const person2: myObiect = {
    name: 'hzw'.age: 18};Copy the code

Differences between type declarations and interfaces:

Type declarations cannot be duplicated The interface can be duplicated and the content is automatically merged

  interface myObiect {
    name: string;
  }
 interface myObiect {
    age: number;
  }
  const person2: myObiect = {
    name: 'hzw'.age: 18};Copy the code
  • An interface is mainly responsible for defining the structure of a class. An interface can limit the interface of an object: an object can match an interface only if it contains all the attributes and methods defined in the interface.
  • Interfaces only define structures without considering actual values;
  • An interface functions like an abstract class, except that all methods and attributes in an interface have no real values. In other words, all methods in an interface are abstract.
interface myClass {
  name: string;
  bark(): void;
}
Copy the code

When defining a class, you can make it implement an interface using the implements keyword

  class class1 implements myClass {
    name: string;
    constructor(name: string) {
      this.name = name;
    }
    bark() {
      console.log('Want want Want'); }}Copy the code

Use an interface to define the type of a function

interface ISum {
  (x: number, y: number): number;
}
let add: ISum = (x: number, y: number): number= > {
  return x + y;
};
Copy the code

When defining a function or class, generics can be useful in cases where the exact type to be used is uncertain (the types of return values, parameters, and attributes are uncertain).

For example, the test function has an indeterminate parameter type, but if it does, it returns the same type as the parameter.

We use any for both the argument and the return value because the type is uncertain, but this is clearly not appropriate:

Using any turns off type checking for TS, and does not indicate that the parameter and return value are of the same type.

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

Generic function

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

Here

is generic, so how to use the above function?

Method 1 (direct use) :

When used, you can pass arguments directly and the type will be inferred automatically by TS, but sometimes the compiler cannot infer automatically

test(10)
Copy the code
Method 2 (Specify type) :

You can also specify generics manually after a function;

test<number>(10)
Copy the code

Function to declare more than one generic type

Multiple generics can be specified at the same time, separated by commas

function test<T.K> (a: T, b: K) :K{
  return b;
}

test<number, string>(10."hello");
Copy the code

A generic class

Classes can also use generics:

class MyClass<T>{
  prop: T;

  constructor(prop: T){
      this.prop = prop; }}Copy the code

Generic inheritance

You can also constrain the scope of generics

T extends MyInter means that T must be a subclass of MyInter. It does not have to be an interface class as well as an abstract class.

interface MyInter{
  length: number;
}

function test<T extends MyInter> (arg: T) :number{
  return arg.length;
}
Copy the code