feat: New array-style overloading

Step two didn't work so well, so this is actually step three. Avoids
  too much redundant type information.
This commit is contained in:
Glen Whitney 2022-09-25 10:39:16 -04:00
parent c4bb415b5e
commit 8a2ae79c90
5 changed files with 138 additions and 4 deletions

17
src/util/overload.ts Normal file
View file

@ -0,0 +1,17 @@
type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void)
? I
: never;
export default function overload<T extends readonly [...any[]]>(
imps: T): UnionToIntersection<T[number]> {
return <any>((...a: any[]) => {
for (let i = 0; i < imps.length; ++i) {
try {
const val = imps[i](...a)
return val
} catch {
}
}
})
}