-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type hole: A function with const generic does not infer the type if it is a spread parameter (tuple) #55033
Comments
There is no bug in your simplified example. You should write this: function unexpectedBehaviour<const K extends readonly unknown[]>(...args: readonly [...K]) {
return {} as K
} From the PR that introduced this feature:
The other problem with this example is that you provide the explicit type argument to your call, one that is using a wider type. I'm not sure why do u expect other things here but if you provide explicit type arguments then inference doesn't even kick in so it's not a surprise that TS doesn't narrow the type based on the provided arguments. However, the behavior with the showcased |
So the problem is that This is a minimal repro case of the problem: TS playground. |
Thank you @Andarist, that was quick! function factory<const T extends unknown[]>(cb: (...args: T) => void) {
return function call<const K extends T>(...args: readonly [...K]) {
return {} as K
}
}
interface Options {
test?: number,
herp?: string
}
const caller = factory((options: Options) => {})
const t1 = caller({ derp: 'asd' })
// ^ '{ derp: string; }' is not assignable to parameter of type 'Options'
const t2 = caller({ test: 123, derp: 'asd' })
// This is a assignable to Options?
const t3: Options = { test: 123, derp: 'asd' }
In this example it seems that I would have expected it to always conform to the shape of My IDE does not provide intellisense for the first property neither (running TS Nightly): Providing an invalid first property does give an error: If I put the cursor in front as shown here, it actually does show the correct (and expected) properties: Should I file this as a separate issue on microsoft/TypeScript? |
Extra keys are always allowed. What you see is the excess property check which is more of a lint rule error than a type system violation. To truly check for assignability I recommend this: type Test = Source extends Target ? 1 : 0 // assignable if 1, not assignable if 0 |
I really wish the EPC error would use different wording than "X is not assignable to Y"; it's misleading and confuses people into thinking there's a bug when they later discover that X sometimes is assignable to Y. |
btw, the |
Everything here is working as intended for various reasons discussed above |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
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
const
in function genericsconst
⏯ Playground Link
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
🙁 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.The text was updated successfully, but these errors were encountered: