July 2018
Intermediate to advanced
420 pages
8h 46m
English
In TypeScript, it is mandatory to define the type of the return of a function. When we have a function that does not have a return, we use a type called void.
Let's see how it works:
Create a new file called void.ts inside the chapter-02 folder, and add the following code:
function myVoidExample(firstName: string, lastName: string): string { return firstName + lastName;}console.log(myVoidExample('Jhonny ', 'Cash'));In the preceding code, everything is OK, because our function returns a value. If we remove the return function, we will see the following error message:
void.ts(1,62): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
In VS Code, you would see the following:
Read now
Unlock full access