使用联合类型

❌ 不要只在一个参数位置编写因类型而异的重载:

Don't write overloads that differ by type in only one argument position:

/* WRONG */
interface Moment {
  utcOffset(): number;
  utcOffset(b: number): Moment;
  utcOffset(b: string): Moment;
}

Mss9qJXz84BwvG69tjtQhRjiVPRQlIL/V1mVRVLuRKtjau63X8cx3MvGC4A+3ezMQ2q3RkfeFgz93ncdxvZuhOqrBGt0dslhWOmlhoz/uojwJg66ODsK4SFEg7HOqt0b+uNjcw1mUal2JtW0aF3AZUY0SHnR7eoXNinKIzdJDFY=

/* OK */
interface Moment {
  utcOffset(): number;
  utcOffset(b: number | string): Moment;
}

Aeb32jjQt/l6qtAQtxvc3x92ZM03w6p6NvmbKZSomt2aBAIgQ1lvpvxonzjdRbJtei0U/TJ7Xl+4rXzkWgFRI3XV3XqEvPcjZ6J14IiiveFjUNGpe+zJnX8kGNk2smAdfkcwGyRhjLd6Ts4toAnsLbHxYP8smTo6ydTus2WYunBYchHszMcFrCNyrN6PDW2FjeE9aTHBR9s8yzdcXt/rVtR2S7iOsep37+FEtU1B9hX94kyiis5uTdY4R/DVwHom8ZPOlWswmFGbaHM5eLIl2Ip4lt35m7n0PCSD6tZJUd9NoZV4wfT5qAK/gb0sx1dj

Oi9dhLij2SfRnuvqtuN3TTb9Qmd+ZSrnkSvMy8SIvxd4PJDwoYUbh1OHJC2iIez7y13QYbXMrgT3wlwPsk8ORYRlu3XCZz1tzdYQjBMToDcjf26WfllejXRcGYy8Fz/DpHX42r/PszO4sV0mhqG5sV60e8yILcBlWmgCn5nVmZjbMUyainnEpG1twP2TTNq5CfpgsMTqcbMzRD8KHZ13Em/XjsSV0KT3za6XkjjDv/uLR+p1JjNrEzWqCAev+UkHbNzML94HnuMw1qy0mikt/sk+KSjucr1iVV9NxkBOA+/DxtarF3OZKHyZrgeSjk05

function fn(x: string): void;
function fn(x: number): void;
function fn(x: number | string) {
  // When written with separate overloads, incorrectly an error
  // When written with union types, correctly OK
  return moment().utcOffset(x);
}