使用可选参数
❌ 不要编写多个仅尾随参数不同的重载:
❌ Don't write several overloads that differ only in trailing parameters:
/* WRONG */
interface Example {
diff(one: string): number;
diff(one: string, two: string): number;
diff(one: string, two: string, three: boolean): number;
}
GvHQ3hoCT0iSjOwhpTYJzxkAm15jziLdeBrodUXEnLRcshcYjAgqYGaqsrPURdpn/AyoMEioDYXGMQ98YuFZ9qoUUXnBVnsMPZRAUHVuS6cEHTAGWnYl8vYwbxiNMiolGec0i+TEK7TnpYtYHnGWSct5PryMRs87egm/9wc16NA=
/* OK */
interface Example {
diff(one: string, two?: string, three?: boolean): number;
}
xUE7VV8x6C4lWZXabkWZouQuv3qJpKr5RNiP6PHdH94fsXnVpzpBukdRlDRx8kw9dMUt3yyMtMd/xd01mdlPMSehpVTj5b4ZaOcJToluTBoTEU/NQ+fwiwXIUa3hbo3Jca7Z4pumkUWFF7HxlGj+YNgg3YBP1kJ1PnMaheq5nyRmpaYuHjIdE9/ZF46VI1tfOX2Lom4aap5zp3So/JCiq6JG4iCWkWVDq3dFomsXCK5K9tgSGoMCrwFz5aUJiZWMELs/WaheED+LPrj48S9Y/A==
+1HLymPpdJMCD27e9Ejj9Y6dh7dkXNwY5VLX0e0w5yqctCTFObTnHmoT+N25j/9yofnN3M9aeCE4Wl2dANKXNGNp6ykbWTbiCWNG4jkP379T9Gld0ogZmAq88XiqmvncrD8UfWVM+tfY5mDxwH01BirY9TBimbPtNPW590HL9i7Xz0JAMt8rncLcCxvS5ZvacT3/YTDR7a6n40NnnQhsVw==
kETPT4ngibUjQDTSLqir3rRaM9j50cQE9sc/B6jkxpu/upxMlvp0qrHFzVogIjYBz7ORLOfbAZ+LNrafVlT64ifJ2cl/btEjW3PWgRnKFHjIIuNJOdN7bhE398ZJYyV5ePHbjLtm2YW9UU+T+2X2qVn6j99y5KIe4QkJhY+0hvPOL8Cwhkds7pTZon7BN2m8pbtONxkZBivJMEFtf3D7O0UfeuaQCy73o/HOwXBa3w9JMDE7qTlOPtODMuYNCAvrjp9619wpOJ40kbZXpibz60VjGSHW8o85ZYkdTljfYQCZ1+IiUuYFJ7WeReOfnx74MpOE1CwrBfHgOGy/8BQK9VUtto2ZVkhTMwMBNxEcBdYryzK1M930ttNgf8z6PRq6Ylsxi0MtGAsx4QvEqbTTY2T1OxJieO0qnWPoNqbj/lxDa6HYl9Sme2me3m1+G1xJTGDhktCHHI+C27TJbmLlHKoEHaFE1Z15Uux/6JQc1Ma7cE0W9TuPZLz6VpePZuAsyfCSvj/1oA0HIjToVcRFSkrDl2zZNFWU310egQoWKAz71hQCUvVQswPEGqC82hAnLDwm7/uEpch9V/URuFzlXY8BVFo7RNzPKMGFbLk95XLEtE/Uidg+eGhCaog1dqoNwcNtmLaifgT9q/7EjSiffWYz4On6BBQSCDgwF9YlodLsx0mXyY0fjODcUHGCtQ6xpHQuBMpKZ8PNqiSSAwPz0jgNkK24KaNFV4/+xkeu/0E=
function fn(x: (a: string, b: number, c: number) => void) {}
var x: Example;
// When written with overloads, OK -- used first overload
// When written with optionals, correctly an error
fn(x.diff);
qXvt2oOrnK/QticIENZOSQgd09rSB1wyPNXsV/dG7LXQ4zji1xHMJIXlVVjccSeaFGHok4z+EaJpDJXwenOu/qwezS95ghyFr39JuQpmLBxa49j0srOQrwylWckrLseNxPVtSlbQEYHne2uFQAbizOMrR7SiLM9TuCFcTzsEGm9ky8152HQlaoLdXoO9osBtHKCrU/oYZLtYJIHcCPEhhBDEqkTico1kUAJ1jKPSNf+cJa2+X8dvcNWQWOun/k6Hkgk0AE2QcPKc9cybmuZb+amO+OlzL/fNviFo8/U0q2MhIOQXOAAfFFLQN5CHORMP/wgLFX/SO6M0Clp7tULUt/ndmoJ9lI2/pvD7ZVjiNo2Yu/blfYOAXPYcaZhVMTa0lNMwbzziR0j6eEPZDrd9z+mUWdJDuo48hrygUp+i977zmaroMJcpJQqmFJ3ubiPic9Y//tRHsT4K1T9huB4ZpOk5Z8eJG2QMboWg92Sft7zmJcZhEgG8OPHf/NO4zRRK8EHB1DOXwrIsfWVWKgzrfQEwgBXHO5ou97Jy2wRavyGZMtD4zigo+Sk0G0SCENiQdjXrhSwO/8KXGUD1C6m65jj+4IoYaZYM5y7Pp0kDI8t93C9dw+8gpz7oVXK5DZeToHiA0JEF2l1uZ+zbyyabEXDFM+4yVmtpywSH2Ip5BcQQbzdMT/s/ZjLajZ6SQyVMr+PmWNJc/IHvcxwIZgPGt0rUloBKAJUhKXkUga0Nt3yWa87IWRHyM4NGKaEkq/+p3wPOG7qRtM+7w3bjZvMmP2Eez1/h+FlbEu1RHcqpfdXlmBkKrhKMnDyD8AuCrtBncpzpERv1TdXhMBMYf8JD68Ejh2lhFJkY20AW0VYdmCfKQUEQIADRdKNtjfOpdD2q
var x: Example;
// When written with overloads, incorrectly an error because of passing 'undefined' to 'string'
// When written with optionals, correctly OK
x.diff("something", true ? undefined : "hour");