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:
parent
c4bb415b5e
commit
8a2ae79c90
5 changed files with 138 additions and 4 deletions
17
src/util/overload.ts
Normal file
17
src/util/overload.ts
Normal 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 {
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue