请签名如下:

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

我如何调用函数error()不指定标题参数,但设置autoHideAfter说1000?


当前回答

你可以使用可选变量by ?或者如果你有多个可选变量。,例如:

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

在上述:

必须填写姓名 Country是必选项,有默认值 地址可选 嗜好是一个可选参数数组

其他回答

您可以创建一个helper方法,该方法根据错误参数接受一个对象参数

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

 getError(args: { message: string, title?: string, autoHideAfter?: number }) {
    return error(args.message, args.title, args.autoHideAfter);
 }

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

在JavaScript中,每个参数都是可选的,用户可以根据自己的需要去掉它们。当它们这样做时,它们的值是未定义的。我们可以在TypeScript中通过添加?我们希望参数的末尾都是可选的。例如,我们希望上面的last name参数是可选的:

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

这与@Brocco的回答几乎相同,但略有不同:只在对象中传递可选参数。(并使params对象可选)。

它最终有点像Python的**kwargs,但又不完全是。

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:');

不幸的是,TypeScript中没有这样的东西(更多细节请访问:https://github.com/Microsoft/TypeScript/issues/467)

但是为了解决这个问题,你可以把你的参数改成一个接口:

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

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

//then to call it:
error({message: 'msg', autoHideAfter: 42});

你可以在接口上指定多个方法签名,然后在类方法上重载多个方法:

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
    }
}

现在所有这些都可以工作了:

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);

...INotificationService的error方法将有以下选项:

操场上