// define your parameter's type inside the parenthesis // define your return type after the parenthesis function sayHello(name: string): string { console.log(`Hello, ${name}`!); } sayHello('Bob'); // Hello, Bob!
type return_type = ReturnType<() => string>; // string // or function hello_world(): string { return "hello world"; } type return_type = ReturnType<typeof hello_world>; // string
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); // not OK foo.save(numCallback); // OK
interface Date { toString(): string; setTime(time: number): number; // ... }
function sayName({ first, last = 'Smith' }: {first: string; last?: string }): void { const name = first + ' ' + last; console.log(name); } sayName({ first: 'Bob' });
function add(x: number, y: number): number { return x + y; }