Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Gaëtan Redin
Gaëtan Redin

Posted on • Originally published atMedium on

TypeScript Function Overloads

When to use TypeScript Function Overloads

I have to admit that I found out about this feature recently. But I would like to share it with you because I find it really easy to understand and simplify code. But like all features, it has its own use cases, and sometimes we should not use this syntax.

When Should We Use TypeScript Function Overloads

Suppose you have two ways to get a user – by its id, which is a number – or by its name (string), first name (string), and birthdate (Luçon DateTime).

Here we have the same method with different parameters.

A first approach could be:

function getUser(idOrName: string | number, firstName?: string, birthDate?: DateTime): User {  ...}
Enter fullscreen modeExit fullscreen mode

But it’s not really relevant because it means that we could also use it like this:

getUser(name, firstName);
Enter fullscreen modeExit fullscreen mode

And that’s not what we expected to write.

We only want to can do:

getUser(id);getUser(name, firstName, birthDate);
Enter fullscreen modeExit fullscreen mode

So this is how to implement what we expect:

getUser(id: number): User; // (1)getUser(name: string, firstName: string, birthDate: DateTime): User; // (2)getUser(idOrName: string | number, firstName?: string, birthDate: DateTime): User {  if(typeof idOrName === number) {    (1)  } else {    // (2)  }}
Enter fullscreen modeExit fullscreen mode

Rules:

  • The implementation must handle all cases.
  • The implementation signature must be the last.

When Should We Not Use TypeScript Function Overloads

Suppose now we have an interface Identity that contains name, first name, and birthDate. It could be tempting to write this:

interface Identity {  name: string;  firstName: string;  birthDate: DateTime;}getUser(id: number): User;getUser(identity: Identity): User;getUser(idOrIdentity: number | Identity): User {  ...}
Enter fullscreen modeExit fullscreen mode

But that’s not the simplest way to code this. That’s better:

interface Identity {  name: string;  firstName: string;  birthDate: DateTime;}getUser(idOrIdentity: number | Identity): User {  ...}
Enter fullscreen modeExit fullscreen mode

You should always prefer the union type when it’s possible. This is a Typescript recommendation, and as you can see, it’s more simple.

Remember that function overload is useful when you don’t have the same number of parameters.

Thanks for reading.

Learn More

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Angular developer who love to share. Still seeking quality code and tips
  • Location
    Nantes, France
  • Work
    Lead dev on Angular project
  • Joined

More fromGaëtan Redin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp