functionpadding(a:number, b?:number, c?:number, d?:any) {if (b ===undefined&& c ===undefined&& d ===undefined) { b = c = d = a; }elseif (c ===undefined&& d ===undefined) { c = a; d = b; }return { top: a, right: b, bottom: c, left: d };}
// Overloadsfunctionpadding(all:number);functionpadding(topAndBottom:number, leftAndRight:number);functionpadding(top:number, right:number, bottom:number, left:number);// Actual implementation that is a true representation of all the cases the function body needs to handlefunctionpadding(a:number, b?:number, c?:number, d?:number) {if (b ===undefined&& c ===undefined&& d ===undefined) { b = c = d = a; }elseif (c ===undefined&& d ===undefined) { c = a; d = b; }return { top: a, right: b, bottom: c, left: d };}
ここで最初の3つの関数ヘッダは paddingへの有効な呼び出しとして利用できます:
padding(1); // Okay: allpadding(1,1); // Okay: topAndBottom, leftAndRightpadding(1,1,1,1); // Okay: top, right, bottom, leftpadding(1,1,1); // Error: Not a part of the available overloads