-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed as not planned
Closed as not planned
Copy link
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
Bug Report
🔎 Search Terms
is:issue is:open function generic tuple const
is:issue is:open in:title tuple const
Might solve; #54254 <T extends [a: string, b: number]>(...args: T)
🕗 Version & Regression Information
- This is a overlooked type feature
- This has been an immediate effect of including a new feature:
const
in function generics - This is the behaviour in every version I tried, and I reviewed the FAQ for entries about
const
⏯ Playground Link
The playground contains both a simplification and the actual use case.
The simplified example shows the core issue, that rest parameters will not be inferred using the function generic const
.
The usage example is to demonstrate relevance in a factory pattern, and to ensure test coverage if necessary.
💻 Code
// * --- Simplified example --- * //
function expectedBehaviour<const K>(example: K) {
return {} as K
}
const ok1 = expectedBehaviour({ test: 1 })
// ^?
function unexpectedBehaviour<const K extends unknown[]>(...args: K) {
return {} as K
}
const notOk1 = unexpectedBehaviour<[{ test: number }]>({ test: 1 })
// ^?
// What it is: const notOk1: [{ test: number }]
// What was expected: const notOk1: [{ test: 1 }]
// * --- Usage example --- * //
function factory<const T extends unknown[]>(cb: (...args: T) => void) {
return function call<const K extends T>(...args: K): K {
// ^ Remove me
return {} as K
}
}
const t1 = factory((a: { test: number }, b: string) => {})({ test: 123 }, 'some string')
// ^?
const t2 = factory((a: { test: number }, b: string) => {})({ test: 123 } as const, 'some string')
// ^?
🙁 Actual behavior
What
A generic function does not apply const
to contents of a rest parameter.
function fn<const K extends unknown[]>(...args: K): K
Why
It's a bug, as the tuple does not become literal.
🙂 Expected behavior
Function generic const
makes a type literal, including rest parameters.
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug