【发布时间】:2015-08-24 10:00:04
【问题描述】:
给出以下签名:
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
我怎样才能调用函数error() 不指定title参数,但设置autoHideAfter说1000?
【问题讨论】:
标签: typescript
给出以下签名:
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
我怎样才能调用函数error() 不指定title参数,但设置autoHideAfter说1000?
【问题讨论】:
标签: typescript
如documentation 中指定,使用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);
【讨论】:
? 被添加到函数definition 中,但问题是关于实际调用 函数。
undefined 才能获得与完全省略参数相同的行为。只是“任何不真实的东西”都行不通,因为 TypeScript actually 与 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});
【讨论】:
另一种方法是:
error(message: string, options?: {title?: string, autoHideAfter?: number});
所以当你想省略标题参数时,只需像这样发送数据:
error('the message', { autoHideAfter: 1 })
我更喜欢这个选项,因为允许我添加更多参数而无需发送其他参数。
【讨论】:
title?
..., options?: {title?: string} = {title: "my default"}, ...
您可以通过? 使用可选变量,或者如果您有多个可选变量通过...,例如:
function details(name: string, country="CA", address?: string, ...hobbies: string) {
// ...
}
在上面:
name 为必填项country 是必需的并且有一个默认值address 是可选的hobbies 是一个可选参数数组【讨论】:
这与@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);
...和INotificationService 的error 方法将有以下选项:
【讨论】:
您可以创建一个辅助方法,根据错误参数接受一个对象参数
error(message: string, title?: string, autoHideAfter?: number){}
getError(args: { message: string, title?: string, autoHideAfter?: number }) {
return error(args.message, args.title, args.autoHideAfter);
}
【讨论】:
在 TS 中,您还可以将参数设置为 Object,并使对象的值可选,这样您就不必定义每个参数,只需定义您想要使用的参数。
public functionBeingCalled(obj: {status?: number, error?: string, message?: string}) {
if(obj.message) { console.log(obj.message) }
}
this.functionBeingCalled({message: 'Error Detected'})
【讨论】:
在这种情况下,您可以为那些不想覆盖的可选参数使用“未定义”值
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
你可以像这样调用错误方法
error("it works", undefined, 20);
小心null 在这里不起作用。
【讨论】:
您可以在没有界面的情况下执行此操作。
class myClass{
public error(message: string, title?: string, autoHideAfter? : number){
//....
}
}
使用? 运算符作为可选参数。
【讨论】:
message 和autoHideAfter
您可以尝试将 title 设置为 null。
这对我有用。
error('This is the ',null,1000)
【讨论】: