How to pass optional parameters in TypeScript while omitting some other optional parameters?
鉴于以下签名:
1 2 3 | export interface INotificationService { error(message: string, title?: string, autoHideAfter?: number); } |
如何调用函数
如文档中所述,使用
1 2 3 4 5 6 7 8 9 10 11 | 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)
但是为了解决这个问题,你可以将你的参数改为接口:
1 2 3 4 5 6 7 8 9 10 11 12 | export interface IErrorParams { message: string; title?: string; autoHideAfter?: number; } export interface INotificationService { error(params: IErrorParams); } //then to call it: error({message: 'msg', autoHideAfter: 42}); |
您可以使用
1 2 3 | function details(name: string, country="CA", address?: string, ...hobbies: string) { // ... } |
在上面:
-
name 是必需的 -
country 是必需的并且具有默认值 -
address 是可选的 -
hobbies 是可选参数的数组
另一种方法是:
1 | error(message: string, options?: {title?: string, autoHideAfter?: number}); |
因此,当您想省略title参数时,只需发送如下数据:
1 | error('the message', { autoHideAfter: 1 }) |
我宁愿这个选项因为允许我添加更多参数而不必发送其他参数。
这与@Brocco的答案几乎相同,但略有不同:只传递对象中的可选参数。 (并且还使params对象可选)。
它最终有点像Python的** kwargs,但并不完全如此。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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:'); |
您可以在接口上指定多个方法签名,然后在类方法上有多个方法重载:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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 } } |
现在所有这些都可行:
1 2 3 4 5 | 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); |
...
操场
您可以创建一个辅助方法,该方法接受基于错误参数的一个对象参数
1 2 3 4 5 | error(message: string, title?: string, autoHideAfter?: number){} getError(args: { message: string, title?: string, autoHideAfter?: number }) { return error(args.message, args.title, args.autoHideAfter); } |
你可以尝试将title设置为null。
这对我有用。
错误('这是',null,1000)
您可以在没有界面的情况下执
1 2 3 4 5 | class myClass{ public error(message: string, title?: string, autoHideAfter? : number){ //.... } } |
使用