如何在TypeScript中传递可选参数,同时忽略其他一些可选参数?

鉴于以下签名:

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

我怎样才能调用函数error() 指定标题参数,但设置autoHideAfter说1000?

如文档中所述,使用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); 

游乐场的链接 。

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

但是为了解决这个问题,你可以改变你的params作为一个接口:

 export interface IErrorParams { message: string; title?: string; autoHideAfter?: number; } export interface INotificationService { error(params: IErrorParams); } //then to call it: error({message: 'msg', autoHideAfter: 42}); 

你可以使用可选的variables? 或者如果您有多个可选variables,例如:

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

在上面:

  • name是必需的
  • country是必需的,并具有默认值
  • address是可选的
  • hobbies是一系列可选参数

您可以指定多个函数types :

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

…和INotificationServiceerror方法将有以下选项:

重载智能感知

操场

这几乎和@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:'); 

你可以做到这一点,没有一个接口。

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

使用? 运算符作为可选参数。