“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

More on Functions

Review of the previous section — ts-02 basic syntax starting from 0

More information on Functions

Function Type Expressions

Define type as function type for method parameter

We can also define function types using aliases

Call Signatures

Functions in JS can have attributes in addition to being called. The call signature scheme is designed for this purpose.

The call signature is not quite the same as the function type; instead of =>, it is:

Construct Signatures

If you want to mark the type of the constructor, you can create it using the construction signature.

Generic Functions

Usually we need to write a function that relates an input type to an output type,

In TS, generics are used to describe the correspondence between two values. We do this by declaring a type parameter in the function signature:

We can call it directly when we use it, the code is as follows

const s = firstElement([1.2.3]);
// Here the type appears
Copy the code

We can also use multiple type parameters as follows

Constraints

The generic function we wrote above can handle any type of value, but sometimes we want to associate two values, but only on a subset of them. In this case, we can use constraints to limit the types of types that type parameters can accept.

You need to pass in a method that conforms to the convention to process the constraint, otherwise the following error occurs

Specifying Type Arguments

Guidelines for Writing Good Generic Functions

  • Push Type Parameters Down

If possible, use the type parameter itself rather than constraining it

  • Use Fewer Type Paramenters

Always use type parameters as little as possible

  • Type Parameters Should Appear Twice

If a type parameter appears in only one place, strongly reconsider whether it is actually needed

Parameter Description Optional Paramenters This parameter is Optional

All optional arguments in ts can be used? Is marked by

Function Overloads

A function implementation with compatible signatures can be implemented at the same time

Overload Signatures and the Implementation Signature

The overload signature is the signature defined, and the implementation signature is the signature implemented in the third line of the method above. The implementation signature must be compatible with the overload signature.

Writing Good OverLoads

As with generics, there are guidelines to follow when using overloading. Following these principles will make your functions easier to call, easier to understand, and easier to implement.

  • If possible, always prefer to use arguments of the union type rather than overloading.

Other Types to Know About

void

  • Void is not the same as undefined

object

  • object is not Object. Always use object!

unknow

  • An unknown type represents any value. This is similar to any type, but more secure because it is illegal to use unknown values.

never

  • Some functions never return a value

Function

  • Global function describes properties like bind, call, apply, etc. that appear on all function values in Javascript. It also has a special property that values of type Function can always be called.

Rest Paramenters and Arguments Rest (parameters and Arguments)

Assignability of Functions

Return Type void

Returning a value of void returns some unusual but expected behavior, and a return type with void can cause an error in any context that returns a type.

Next section — The ts-04 object type starts at 0

— END —