Reusable Types (Type Aliases)
Documentation
Anywhere a greeting is expected, you can provide a
string
, a function returning astring
, or aGreeter
instance.
Code
1 2 3 4 5 6 7 8 | 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:
1 2 3 | type GreetingLike = string | (() => string) | Greeting; declare function greet(g: GreetingLike): void; |
Please login to continue.