签名如下:

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

如何调用函数error() not指定title参数,但将autoHideAfter设置为1000

推荐答案

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

Playground link

Typescript相关问答推荐

相交TypScript中的对象和接口

TypScript如何在 struct 上定义基元类型?

TypeScript实用程序类型—至少需要一个属性的接口,某些指定属性除外

仅针对某些状态代码重试HTTP请求

如何将泛型对象作为返回类型添加到类型脚本中

是否存在类型联合的替代方案,其中包括仅在某些替代方案中存在的可选属性?

我可以以这样一种方式键入对象,只允许它的键作为它的值吗?

React Typescript项目问题有Redux-Toolkit userSlice角色问题

有没有可能为这样的函数写一个类型?

一个打字类型可以实现一个接口吗?

以Angular 自定义快捷菜单

对象类型的TypeScrip隐式索引签名

为什么上下文类型在`fn|curred(Fn)`的联合中不起作用?

我不明白使用打字脚本在模板中展开参考

必需的输入()参数仍然发出&没有初始值设定项&q;错误

三重融合视角与正交阵

是否可以定义引用另一行中的类型参数的类型?

Typescript泛型验证指定了keyof的所有键

如何使用 runInInjectionContext 中的参数测试功能性路由防护

Angular另一个组件的nativeelement未定义