typescript create function code example

Example 1: types function typescript

interface Safer_Easy_Fix {
    title: string;
    callback: () => void;
}
interface Alternate_Syntax_4_Safer_Easy_Fix {
    title: string;
    callback(): void;
}

Example 2: simple function in typescript

// Named function

//function with type as number
function add(x: number, y: number): number {
  // return sum of numbers entered as params
  return x + y;
}

// Anonymous function

// variable to call and define function
let myAdd = function (x: number, y: number): number {
  // return sum of numbers entered as params
  return x + y;
};

Example 3: types function typescript

interface Easy_Fix_Solution {
    title: string;
    callback: Function;
}

Example 4: types function typescript

interface Alternate_Syntax_4_Advanced {
    title: string;
    callback<T extends unknown[], R = unknown>(...args?: T): R;
}