1interface Safer_Easy_Fix {
2 title: string;
3 callback: () => void;
4}
5interface Alternate_Syntax_4_Safer_Easy_Fix {
6 title: string;
7 callback(): void;
8}
9
1// Parameter type annotation
2function greet(name: string): string {
3 return name.toUpperCase();
4}
5
6console.log(greet("hello")); // HELLO
7console.log(greet(1)); // error, name is typed (string)
1// define your parameter's type inside the parenthesis
2// define your return type after the parenthesis
3
4function sayHello(name: string): string {
5 console.log(`Hello, ${name}`!);
6}
7
8sayHello('Bob'); // Hello, Bob!
1// Named function
2
3//function with type as number
4function add(x: number, y: number): number {
5 // return sum of numbers entered as params
6 return x + y;
7}
8
9// Anonymous function
10
11// variable to call and define function
12let myAdd = function (x: number, y: number): number {
13 // return sum of numbers entered as params
14 return x + y;
15};
1// Named function
2function add(x: number, y: number): number {
3 return x + y;
4}
5
6// Anonymous function
7let myAdd = function (x: number, y: number): number {
8 return x + y;
9};