typescript type function code example
Example 1: typescript optional parameters
sayHello(hello?: string) {
console.log(hello);
}
sayHello();
sayHello('world');
Example 2: typescript default parameter
sayHello(hello: string = 'hello') {
console.log(hello);
}
sayHello();
sayHello('world');
Example 3: types function typescript
interface Safer_Easy_Fix {
title: string;
callback: () => void;
}
interface Alternate_Syntax_4_Safer_Easy_Fix {
title: string;
callback(): void;
}
Example 4: typescript parameter function type
class Foo {
save(callback: (n: number) => any) : void {
callback(42);
}
}
var foo = new Foo();
var strCallback = (result: string) : void => {
alert(result);
}
var numCallback = (result: number) : void => {
alert(result.toString());
}
foo.save(strCallback);
foo.save(numCallback);
Example 5: typescript function
function greet(name: string): string {
return name.toUpperCase();
}
console.log(greet("hello"));
console.log(greet(1));
Example 6: typescript function type
interface getUploadHandlerParams {
checker : Function
}