Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d6a104

Browse files
committed
feat(question): add #33823 - OverloadedParameters
1 parent 9080836 commit 0d6a104

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
TypeScript’s `Parameters` don’t support overloaded functions, causing issues with type narrowing and undefined parameters during destructuring. The goal is to create utility types that extract parameters from all overloads, eliminating the need for `?.` and `!.` after type narrowing.
2+
3+
```
4+
function func(fn: (arg: string) => string): string
5+
6+
function func(num: number, init: Set<string>): boolean
7+
8+
function func(...args: unknown[]): string | boolean {
9+
const [fnOrNum, init] = args as OverloadedParameters<typeof func>
10+
11+
if (typeof fnOrNum === 'function') {
12+
return fnOrNum("foo")
13+
} else {
14+
return fnOrNum === init.size
15+
}
16+
}
17+
18+
func((s) => `Hello ${s}`)
19+
func(1, new Set<string>('Hi'))
20+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
difficulty: hard
2+
title: OverloadedParameters
3+
author:
4+
github: teamchong
5+
name: Steven
6+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type OverloadedParameters = any
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Equal, Expect } from '@type-challenges/utils'
2+
import { ExpectFalse, NotEqual } from '@type-challenges/utils'
3+
4+
function func(fn: (args: string) => string): string
5+
6+
function func(num: number, init: Set<string>): boolean
7+
8+
function func(...args: unknown[]): string | boolean {
9+
const [fnOrNum, init] = args as OverloadedParameters<typeof func>
10+
11+
if (typeof fnOrNum === 'function') {
12+
return fnOrNum("foo")
13+
} else {
14+
return fnOrNum === init.size
15+
}
16+
}
17+
18+
func((s) => `Hello ${s}`)
19+
func(1, new Set<string>('Hi'))
20+
21+
type cases = [
22+
Expect<Equal<OverloadedParameters<typeof func>, [number, Set<string>] | [(args: string) => string, never]>>,
23+
]

0 commit comments

Comments
 (0)