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

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;
doc_TypeScript
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.