次の署名があるとします。
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
パラメータを指定せerror()
ずにtitle
、autoHideAfter
言うように設定して関数を呼び出すにはどうすればよい1000
ですか?
次の署名があるとします。
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
パラメータを指定せerror()
ずにtitle
、autoHideAfter
言うように設定して関数を呼び出すにはどうすればよい1000
ですか?
回答:
ドキュメントで指定されているように、次を使用します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);
残念ながら、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});
オプションの変数byを使用できます。?
複数のオプションの変数byがある場合は...
、例を示します。
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
は次のオプションがあります:
タイトルをnullに設定してみてください。
これでうまくいきました。
error('This is the ',null,1000)
インターフェースなしでこれを行うことができます。
class myClass{
public error(message: string, title?: string, autoHideAfter? : number){
//....
}
}
?
演算子をオプションのパラメーターとして使用します。
message
とautoHideAfter
?
機能で追加された定義が、質問は実際にについてです呼び出し機能を。