1, function,

Function types consist mainly of parameter types and return values

1-1. Function definition

functionThe method name () : the return value{
   / / the method body
}
Copy the code

There are several common definitions of functions

  • Name definition
function namedAdd(x: number, y: number) :number {
    return x + y
}
Copy the code
  • Anonymous definition
let anonymousAdd = function (x: number, y: number) {
    return x + y;
}
Copy the code
  • Arrow function mode

The full definition of using arrow functions is as follows

letThe method name:(The parameter types) = >Return type =function(Method type): Return type{method body}Copy the code

Example:

let lamAdd: (x: number, y: number) = > number
		 = function (x: number, y: number) :number {
    return x + y;
}
Copy the code

If the return type is not required, simply change number to void.

The above is a complete function definition, which is a bit cumbersome, because we have defined the parameters in detail, and the above method can also be simplified into the following code.

let simple2Add = (x, y) = > x + y
/ / use
simple2Add(2.3)
Copy the code

1-2. Optional parameters

As with optional arguments used in interfaces and classes in the previous article, optional arguments in functions follow the same rules.

Here is a simple example of an optional argument

/ / define
function getScreenResume(name: string, bust? :number, age: number = 24) {
    if (bust) {
        console.log(` d brassiere:${bust}`)}console.log(` name:${name}Age:${age}`)}/ / use
getScreenResume('john')
getScreenResume('john'.96)
getScreenResume('john'.96.43)
Copy the code

Bust and age above are optional parameters and belong to two different types:

  • bust? : number? Methods the optional
  • age: number = 24Default value mode

If optional parameters are used, mandatory parameters must be written before optional parameters; otherwise, compilation errors may occur.

function getScreenResume(name: string, bust? :number,
	 age: number = 24.type: number) {
	// do somethins
}
Copy the code

The code above will alert you to a writing error

TS1016: A required parameter cannot follow an optional parameter
Copy the code

1-3. Remaining parameters

The residual parameter can be understood as another optional parameter. It can be used when multiple data needs to be passed but the length is not determined.

function moreArguments(name: string. others:string[].type: string) {}Copy the code

The remaining parameters must be used as the last parameter of the last function; otherwise, an error will be reported.

TS1014: A rest parameter must be last in a parameter list
Copy the code

1-4. This & arrow function

The arrow function does not catch this

class Handler {
    info: string;
    onClick = (e: Event) = > {  // Here is an arrow function
        this.info = e.type; }}Copy the code

2, paradigm

A paradigm can constrain parameter inputs, parameter outputs, and return results.

2-1. Basic use of the paradigm

function f<T> (arg: T) :T {
    return arg;
}
// Use it correctly
f<string> ('hello')
// Error
f<number> ('hello') //TS2345: Argument of type 
//'string' is not assignable to parameter of type 'number'
Copy the code

A scope can also be defined as a type for a function

let f1: <U>(arg: U) = > U = f
f1<string> ('Hexo'). 'U' must be a string
Copy the code

The function type and return type of f1 are U

2-2. Use generics in interfaces

Stereotypes support inheritance, which enables generic objects to gain more functionality

class Drag {
    public x: number;
    public y: number;

    public move(x: number, y: number) {
        this.x = x
        this.y = y
        console.log('Move to position: x:${x},y: ${y}`)}}Copy the code

Show generic inheritance

interface GenericId<T extends Drag> {
	// This is defined in a very special way
	// A class object is passed in from Java
    show(c: new () => T): T
}
...
class MyID implements GenericId<Drag> {

    show(c: new () => Drag): Drag {
        let drag = new Drag()
        drag.move(2.3)
        let _c = new c()
        _c.move(2.3)
        returndrag; }}Copy the code

use

let myId = new MyID()
myId.show(Drag)
Copy the code