g.p*_*dou 238 typescript
鉴于以下签名:
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
Run Code Online (Sandbox Code Playgroud)
如何调用函数error()而不指定title参数,但将autoHideAfter设置为1000?
Tho*_*mas 257
如文档中所述,使用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);
Run Code Online (Sandbox Code Playgroud)
Bro*_*cco 62
不幸的是,在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});
Run Code Online (Sandbox Code Playgroud)
Has*_*sef 35
您可以使用可选变量,?或者如果您有多个可选变量...,例如:
function details(name: string, country="CA", address?: string, ...hobbies: string) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
在上面:
name 是必须的country 是必需的,并具有默认值address 是可选的hobbies 是一组可选参数Gab*_*ada 14
另一种方法是:
error(message: string, options?: {title?: string, autoHideAfter?: number});
Run Code Online (Sandbox Code Playgroud)
因此,当您想省略title参数时,只需发送如下数据:
error('the message', { autoHideAfter: 1 })
Run Code Online (Sandbox Code Playgroud)
我宁愿使用此选项,因为它允许我添加更多参数而不必发送其他参数。
Mon*_*pit 10
这与@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:');
Run Code Online (Sandbox Code Playgroud)
您可以在接口上指定多个方法签名,然后在类方法上有多个方法重载:
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
}
}
Run Code Online (Sandbox Code Playgroud)
现在所有这些都可行:
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);
Run Code Online (Sandbox Code Playgroud)
...并且error方法INotificationService将具有以下选项:

| 归档时间: |
|
| 查看次数: |
210389 次 |
| 最近记录: |