K
K
kachurinets2020-08-06 14:30:49
Angular
kachurinets, 2020-08-06 14:30:49

How do you define the type of a function in your application?

Often, when writing a function, it is difficult to describe its type when writing a function. If you just set the basic types of typescript, then there is no particular difficulty, but Angular applications also use their own types (for example, FormGroup, ElementRef) and, in addition to all this, types that other libraries use.

Tell me in what way you can quickly and conveniently write the type of a function, variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2020-08-07
@kachurinets

Well, the IDE will tell you when you enter it.)
And what to enter - you should know like this: you know what your function works with.) If, logically, your function should potentially work with something else, but you didn’t know about it at the moment writing, then you didn’t foresee this inside the function too, which means that the absence of the specified type is only a plus.
You can study the types by climbing the .d.ts of the corresponding libs. The IDE will help here too: ctrl+tyk on an existing function will open its declaration.
In general, in TS structural typing , you do not need to know what type names the user is working with, you just need to specify the structure of the desired type.

D
Dmitry Belyaev, 2020-08-06
@bingo347

TypeScript can easily calculate the ReturnType of a function from the type of the expression in return
If you don't know in advance what type it will be - just don't specify it, TS will calculate it itself.
But when the function has users, and you need to fix the ReturnType for the correctness of further changes, then add it so that ts already monitors the type of the expression in return

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question