create function typescript 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: typescript function
function greet(name: string): string {
return name.toUpperCase();
}
console.log(greet("hello"));
console.log(greet(1));
Example 3: typescript default parameter
function sayName({ first, last = 'Smith' }: {first: string; last?: string }): void {
const name = first + ' ' + last;
console.log(name);
}
sayName({ first: 'Bob' });
Example 4: typescript function
function add(x: number, y: number): number {
return x + y;
}
let myAdd = function (x: number, y: number): number {
return x + y;
};
Example 5: simple function in typescript
function add(x: number, y: number): number {
return x + y;
}
let myAdd = function (x: number, y: number): number {
return x + y;
};
Example 6: types function typescript
interface Better_still_safe_but_way_more_flexible_fix {
title: string;
callback: <T = unknown, R = unknown>(args?: T) => R;
}
interface Alternate_Syntax_4_Better_still_safe_but_way_more_flexible_fix {
title: string;
callback<T = unknown, R = unknown>(args?: T): R;
}