microsoft / microsoft/TypeScript
Type inference behaves unexpectedly when dealing with functions that have type parameters
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
When a function definition a with type parameters is passed as an argument to another function b, b is unable to destructure the parameters of the passed a function.
🔎 Search Terms
type parameter function argument destructure
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics
⏯ Playground Link
Playground link with relevant code
💻 Code
type DefaultVariantProps = {
tabsClassName?: string
}
function fn<P extends {}>(arg1?: (props: P) => void): P {
return null as any
}
const a = fn(null as any as (props: DefaultVariantProps) => void)
const b = fn(null as any as <T>(props: DefaultVariantProps) => void)
// no problem
a.tabsClassName
// Property 'tabsClassName' does not exist on type '{}'.(2339)
b.tabsClassName
type T1 = (props: DefaultVariantProps) => void
type T2 = <T>(props: DefaultVariantProps) => void
// no problem
type T3 = Parameters<T1>[0]['tabsClassName']
// no problem
type T4 = Parameters<T2>[0]['tabsClassName']
🙁 Actual behavior
When a function definition a with type parameters is passed as an argument to another function b, b is unable to destructure the parameters of the passed a function.
It seems that generics should not cause type inference failures.
🙂 Expected behavior
No error.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and the code sample, comparing the inferred types of a and b when the passed function has a type parameter. The issue is done when b.tabsClassName produces no error while the existing non-generic case continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100