TypeScript Core Types – Types in function arguments and return value. Number

When you create a function in TypeScript you can specify the data type of the function’s arguments and the data type for the return value

Example:

function sum(x: number, y: number): number {
    return x + y;
}

Here the syntax x: number, y: number means that the function can accept two argumentsx and y and they can only be numbers and (...): number { means that the return value can only be a number

Usage:

sum(84 + 76) // will be return 160

Note:

You can not do so

function sum(x: string, y: string): number {
    return x + y;
}

or

function sum(x: number, y: number): string {
    return x + y;
}

it will receive the following errors:

error TS2322: Type 'string' is not assignable to type 'number' and error TS2322: Type 'number' is not assignable to type 'string' respectively

if you want to reproduce, please indicate the source:
TypeScript Core Types – Types in function arguments and return value. Number - CodeDay