オプションパラメータを渡し、他のオプションパラメータを省略するにはどうすればよいでしょうか? 質問する

オプションパラメータを渡し、他のオプションパラメータを省略するにはどうすればよいでしょうか? 質問する

次の署名があるとします。

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

パラメータを指定error() せずtitleに、 と設定してautoHideAfter関数を呼び出すにはどうすればよいでしょうか1000?

ベストアンサー1

規定に従ってドキュメンテーション、 使用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);

遊び場リンク

おすすめ記事