Answers:
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);
undefined
以具有与完全省去参数相同的行为。仅“任何不真实的内容”将不起作用,因为TypeScript 实际上是比较void 0
安全的书写方式undefined
。 )
不幸的是,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});
您可以使用?
或,如果您有多个可选变量...
,请使用可选变量,例如:
function details(name: string, country="CA", address?: string, ...hobbies: string) {
// ...
}
在上面:
name
是必须的country
是必需的,并且具有默认值address
是可选的hobbies
是可选参数的数组另一种方法是:
error(message: string, options?: {title?: string, autoHideAfter?: number});
因此,当您想省略title参数时,只需发送如下数据:
error('the message', { autoHideAfter: 1 })
我宁愿使用此选项,因为它允许我添加更多参数而不必发送其他参数。
title
?
这几乎与@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:');
您可以在接口上指定多个方法签名,然后在类方法上具有多个方法重载:
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);
...以及的error
方法INotificationService
将具有以下选项:
您可以尝试将title设置为null。
这对我有用。
error('This is the ',null,1000)
您可以在没有界面的情况下执行此操作。
class myClass{
public error(message: string, title?: string, autoHideAfter? : number){
//....
}
}
使用?
运算符作为可选参数。
message
和autoHideAfter
?
其添加到函数定义中,但问题是关于实际调用函数。