microsoft / microsoft/TypeScript
Inconsistency in generic parameter inference
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.8.3, nightly, etc.
Search Terms: infer function return
Code
declare function h<T extends keyof HTMLElementTagNameMap>(
tag: T,
...init: ((e: HTMLElementTagNameMap[T]) => void)[],
): HTMLElementTagNameMap[T];
declare function events<T extends HTMLElement>(
handlers: Partial<{ [K in keyof HTMLElementEventMap]: (this: T, e: HTMLElementEventMap[K]) => void }>
): (e: T) => void;
declare function props<T extends HTMLElement>(
props: Partial<T>
): (e: T) => void;
h(
'input',
events({
input() {
this.value;
},
}),
props({
value: '42'
})
)
Expected behavior:
no errors
Actual behavior:
Argument of type '{ value: string; }' is not assignable to parameter of type 'Partial<HTMLElement>'. Object literal may only specify known properties, and 'value' does not exist in type 'Partial<HTMLElement>'.
Playground Link: playground
Related Issues: I'm pretty sure I didn't choose a decent title for the issue, so my ability to search for this is limited as well.
I can understand this could be viewed as feature request/improvement, but there are a couple of reasons that make the behaviour feel buggy:
- The inference works correctly for the
eventscall, which is resolved asevents<HTMLInputElement>. - The intellisense works on the
propsargument, which means at some point, the compiler understands I'm writing aPartial<HTMLInputElement>. But for some reason, it resolves the call toprops<HTMLElement>, so the code fails to typecheck. I can work around it by adding the type parameter, but it degrades de developing experience.
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 Playground reproduction and compare the inferred types for the events and props calls, especially the final h call. Investigate the TypeScript compiler's generic parameter inference behavior; done means the shown code type-checks without errors while preserving the expected HTMLElementInput inference.
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