Reusable Types (Type Aliases)

Reusable Types (Type Aliases)

Documentation

Anywhere a greeting is expected, you can provide a string, a function returning a string, or a Greeter instance.

Code

function getGreeting() {
  return "howdy";
}
class MyGreeter extends Greeter { }

greet("hello");
greet(getGreeting);
greet(new MyGreeter());

Declaration

You can use a type alias to make a shorthand for a type:

type GreetingLike = string | (() => string) | Greeting;

declare function greet(g: GreetingLike): void;
doc_TypeScript
2016-10-04 19:25:33
Comments
Leave a Comment

Please login to continue.