Create a new index.ts:

type NumGenerator = (input: number) = > number;

function myFunc(numGenerator: NumGenerator | undefined) {
    // Object is possibly 'undefined'.(2532)
    // Cannot invoke an object which is possibly 'undefined'.(2722)
    const num1 = numGenerator(1); // Error
    constnum2 = numGenerator! (2); //OK
    return {
        num_1: num1,
        num_2: num2
    }
}

const jerry: NumGenerator = (input) = > input + 1;

console.log(jerry(1));
console.log(jerry(2));

export class TestClass{
    constructor(){
        console.log(jerry(1));
        console.log(jerry(2));
        console.log(myFunc(jerry)); }}Copy the code

Use type to define a new type that represents a function with an input parameter of type number and a return type of number.

You can then define your own function variables with that type in the same way you would with a normal type.

Final output: