Lynne
0
Q:

typescript function type

// 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!
2
type return_type = ReturnType<() => string>; // string
// or
function hello_world(): string {
  return "hello world";
}
type return_type = ReturnType<typeof hello_world>; // string
1
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
2
interface Date {
  toString(): string;
  setTime(time: number): number;
  // ...
}
1
function sayName({ first, last = 'Smith' }: {first: string; last?: string }): void {
  const name = first + ' ' + last;
  console.log(name);
}

sayName({ first: 'Bob' });
1
function add(x: number, y: number): number {
  return x + y;
}
0

New to Communities?

Join the community