467

Given the following signature:

export interface INotificationService {
    error(message: string, title?: string, autoHideAfter?: number);
}

How can I call the function error() not specifying the title parameter, but setting autoHideAfter to say 1000?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
g.pickardou
  • 32,346
  • 36
  • 123
  • 268

12 Answers12

497

As specified in the documentation, use undefined:

export interface INotificationService {
    error(message: string, title?: string, autoHideAfter? : number);
}

class X {
    error(message: string, title?: string, autoHideAfter?: number) {
        console.log(message, title, autoHideAfter);
    }
}

new X().error("hi there", undefined, 1000);

Playground link.

Willem Renzema
  • 5,177
  • 1
  • 17
  • 24
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 13
    @BBi7 I think you misunderstood the documentation. The `?` is added in the function _definition_, but the question is about actually _calling_ the function. – Thomas Aug 01 '18 at 07:58
  • Hi @Thomas I totally understand the documentation and know you must add the ? after the parameter in the function definition. But, related to calling a function with optional parameter(s) I'd assume passing undefined if not applicable. I generally I try to find ways to make optional parameter(s) as the end parameter(s) so I can just not pass vs. undefined. But obviously if you have many then you'd need to pass undefined or anything non-truthy. I'm not sure what I was referring to back when I originally posted. Meaning I don't know if it was edited but what I see now fine correct. – BBi7 Aug 15 '18 at 14:56
  • 3
    @BBi7 There were no recent edits. Okay, never mind then :) (Note that you actually must pass `undefined` to get the same behaviour as leaving out the argument entirely. Just "anything non-truthy" will not work, because TypeScript [actually](https://www.typescriptlang.org/play/#src=function%20foo(x%3A%20number%20%3D%200)%20%7B%0A%7D) compares to `void 0` which is a safer way of writing `undefined`.) – Thomas Aug 16 '18 at 10:04
  • @Thomas actually this was the crucial information: if you want the default value to apply to say the first argument and then set the second one, you **must** specify `undefined` as the first "value". – Igor Oct 26 '22 at 22:35
107

Unfortunately there is nothing like this in TypeScript (more details here: https://github.com/Microsoft/TypeScript/issues/467)

But to get around this you can change your params to be an interface:

export interface IErrorParams {
  message: string;
  title?: string;
  autoHideAfter?: number;
}

export interface INotificationService {
  error(params: IErrorParams);
}

//then to call it:
error({message: 'msg', autoHideAfter: 42});
Brocco
  • 62,737
  • 12
  • 70
  • 76
  • Could you please tell, can i call the error method as this, error({message : 'test'}), I think, we can't, so error({message : 'test', autoHideAFter: undefined}), but what i'm expecting is error(message), If i didn't pass other paramers, it shold take the values from default parameters. – Cegone May 26 '20 at 14:18
59

Another approach is:

error(message: string, options?: {title?: string, autoHideAfter?: number});

So when you want to omit the title parameter, just send the data like that:

error('the message', { autoHideAfter: 1 })

I'd rather this options because allows me to add more parameter without having to send the others.

You can also use Partial<T> type in method's signature but it this case you have to create an interface for your options:

interface IMyOptions {
  title: string;
  autoHideAfter: number;
}

And then the method's signature can look like:

error(message: string, options?: Partial<IMyOptions>);

Usage is the same as above.

Type Partial<T> should already be declared in global typings as following:

type Partial<T> = {
  [P in keyof T]?: T[P];
};
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
Gabriel Castillo Prada
  • 4,114
  • 4
  • 21
  • 31
39

you can use optional variable by ? or if you have multiple optional variable by ..., example:

function details(name: string, country="CA", address?: string, ...hobbies: string) {
    // ...
}

In the above:

  • name is required
  • country is required and has a default value
  • address is optional
  • hobbies is an array of optional params
martinjbaker
  • 1,424
  • 15
  • 23
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
  • 3
    Shouldn't hobbies be typed as an array ? – ProgrammerPer Nov 07 '18 at 09:16
  • 5
    There's a useful info in this answer, but it doesn't answer the question. The question is, as I see it, how one can bypass/skip several optional parameters and set only specific ones. – MaxB Mar 28 '19 at 13:05
  • 5
    country isn't required, it's an optional param having a default value of 'CA' instead of undefined. if it's required then what's the point of providing default value? – Anand Bhushan Nov 17 '19 at 04:52
15

This is almost the same as @Brocco 's answer, but with a slight twist: only pass optional parameters in an object. (And also make params object optional).

It ends up being kind of like Python's **kwargs, but not exactly.

export interface IErrorParams {
  title?: string;
  autoHideAfter?: number;
}

export interface INotificationService {
  // make params optional so you don't have to pass in an empty object
  // in the case that you don't want any extra params
  error(message: string, params?: IErrorParams);
}

// all of these will work as expected
error('A message with some params but not others:', {autoHideAfter: 42});
error('Another message with some params but not others:', {title: 'StackOverflow'});
error('A message with all params:', {title: 'StackOverflow', autoHideAfter: 42});
error('A message with all params, in a different order:', {autoHideAfter: 42, title: 'StackOverflow'});
error('A message with no params at all:');
Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38
9

You can specify multiple method signatures on the interface then have multiple method overloads on the class method:

interface INotificationService {
    error(message: string, title?: string, autoHideAfter?: number);
    error(message: string, autoHideAfter: number);
}

class MyNotificationService implements INotificationService {
    error(message: string, title?: string, autoHideAfter?: number);
    error(message: string, autoHideAfter?: number);
    error(message: string, param1?: (string|number), param2?: number) {
        var autoHideAfter: number,
            title: string;

        // example of mapping the parameters
        if (param2 != null) {
            autoHideAfter = param2;
            title = <string> param1;
        }
        else if (param1 != null) {
            if (typeof param1 === "string") {
                title = param1;
            }
            else {
                autoHideAfter = param1;
            }
        }

        // use message, autoHideAfter, and title here
    }
}

Now all these will work:

var service: INotificationService = new MyNotificationService();
service.error("My message");
service.error("My message", 1000);
service.error("My message", "My title");
service.error("My message", "My title", 1000);

...and the error method of INotificationService will have the following options:

Overload intellisense

Playground

David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • 11
    Just a note that I would recommend against this and instead pass in an object and put these parameters as properties on that object... it's a lot less work and the code will be more readable. – David Sherret Mar 01 '16 at 17:05
  • This just isn't a good idea. What if I have three string parameters but want to omit only one of the parameters? This won't work as all three overrides would have the same signature – claudekennilol Jul 20 '20 at 18:04
  • The params assignment looks quite fragile and hard to maintain. Besides, if yet another param was needed in the future, it would entail even more juggling. – Javi Marzán Oct 07 '21 at 10:36
3

In TS you could also make your parameters into an Object and make the value of the object optional, that way you don't have to define every parameters, just the one you want to use.

public functionBeingCalled(obj: {status?: number, error?: string, message?: string}) {
  if(obj.message) { console.log(obj.message) }
}

this.functionBeingCalled({message: 'Error Detected'})
Claudio Teles
  • 218
  • 3
  • 9
2

You can create a helper method that accept a one object parameter base on error arguments

 error(message: string, title?: string, autoHideAfter?: number){}

 getError(args: { message: string, title?: string, autoHideAfter?: number }) {
    return error(args.message, args.title, args.autoHideAfter);
 }
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
-1

In such cases you can use "undefined" value for those optional params which you do not want to overwrite

export interface INotificationService {
    error(message: string, title?: string, autoHideAfter?: number);
}

You can call error method like

error("it works", undefined, 20);

Be careful null doesn't work here.

UniCoder
  • 3,005
  • 27
  • 26
-1

https://www.typescriptlang.org/docs/handbook/functions.html

In JavaScript, every parameter is optional, and users may leave them off as they see fit. When they do, their value is undefined. We can get this functionality in TypeScript by adding a ? to the end of parameters we want to be optional. For example, let’s say we want the last name parameter from above to be optional:

function buildName(firstName: string, lastName?: string) {
  if (lastName) return firstName + " " + lastName;
  else return firstName;
}
 
let result1 = buildName("Bob"); // works correctly now
let result2 = buildName("Bob", "Adams", "Sr."); // error, too many parameters
Expected 1-2 arguments, but got 3.
let result3 = buildName("Bob", "Adams"); // ah, just right
Rafiq
  • 8,987
  • 4
  • 35
  • 35
  • [Same comment](https://stackoverflow.com/questions/30734509/how-to-pass-optional-parameters-while-omitting-some-other-optional-parameters#comment85416689_39236251) as for the linked answer: you didn't answer the question, or you didn't read it. – Dan Dascalescu Apr 17 '22 at 18:34
-2

You can do this without an interface.

class myClass{
  public error(message: string, title?: string, autoHideAfter? : number){
    //....
  }
}

use the ? operator as an optional parameter.

Rick
  • 12,606
  • 2
  • 43
  • 41
  • but this doesn't allow you any way to specify `message` and `autoHideAfter` – Simon_Weaver Nov 19 '16 at 02:27
  • 3
    you don't answer the question or you didn't read it. He wants to know how to call a method with multiple optional parameters, without having to specify the first optional if he only want to enter the second one. – Gregfr Mar 10 '18 at 05:43
-3

You could try to set title to null.

This worked for me.

error('This is the ',null,1000)
Anto S
  • 2,448
  • 6
  • 32
  • 50
numpsi
  • 1
  • 1