【TypeScript】関数の引数を省略する
順番通りに引数を省略したい場合: 引数をフラットに設定する
任意の引数を省略したい場合: 引数をオブジェクトに設定する
code:順番通りに引数を省略.ts
function example(a: number, b?: string, c?: boolean) {
console.log(a, b, c);
}
// 呼び出し
example(1); // b, cが省略される
example(1, "hello"); // cが省略される
example(1, "hello", true);
code:任意の引数を省略.ts
type ExampleOptions = {
a: number;
b?: string;
c?: boolean;
};
function example({ a, b, c }: ExampleOptions) {
console.log(a, b, c);
}
// 呼び出し
example({ a: 1 }); // b, cが省略される
example({ a: 1, b: "hello" }); // cが省略される
example({ a: 1, b: "hello", c: true });
public.icon