Probable bug: Functional properties don't get inferred with circular type parameter constraints like `T extends M<T>`
See original GitHub issueTypeScript Version: 4.0.2
Search Terms: Circular type parameter constraint, functional property inference, function parameter inference, function argument inference
Code:
declare const m: <T extends M<T>>(m: T) => T
type M<Self, K = Exclude<keyof Self, "k" | "t">> =
{ a?: number
, b?: number
, c?: number
, d?: number
, k?: K
, t?: (k: K) => void
}
// :)
// Case 1
m({
a: 1,
b: 2,
k: "a"
})
// :)
// Case 2
m({
a: 1,
b: "x", // expected error
k: "c" // expected error
})
// :(
// Case 3
m({ // type parameter becomes `unknown` (probably because t's first parameter is `any` initially kinda?)
a: 1,
b: 2,
k: "a",
t: k => {} // k is inferred as `never` (instead of `"a" | "b"`)
})
// :|
// Case 4
m({
a: 1,
b: 2,
k: "a",
t: (k: "a" | "b") => {} // have to explicitly type `k` which could have been inferred :(
})
Expected behavior:
The parameter k
of t
should be inferred as "a" | "b"
. As property k
already gets inferred in Case 1 & 2, so why not the k
parameter of t
Actual behavior:
The parameter of t
is inferred as never
and T
in inferred as unknown
. I assume first T
gets resolved to unknown
as t
is initially inferred as (k: any) => void
which does not satisfy the constraint of m
.
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (5 by maintainers)
Top Results From Across the Web
Constrain generic type parameter passed to a function
The only way that I've come up with is to infer generic parameters with conditional types and pass them to the function manually:...
Read more >Chapter 18. Type Inference
This section defines inference variables, constraint formulas, and bounds, as the terms will be used throughout this chapter. It also presents notation.
Read more >4. Functions - Programming TypeScript [Book] - O'Reilly
Chapter 4. Functions In the last chapter we covered the basics of TypeScript's type system: primitive types, objects, arrays, tuples, and enums, as...
Read more >Generics - mypy 0.991 documentation
Generic types have one or more type parameters, which can be arbitrary types. ... Note that the generic aliases in typing don't support...
Read more >Documentation - Everyday Types - TypeScript
Even though the parameter s didn't have a type annotation, TypeScript used the types of the forEach function, along with the inferred type...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Repro for the phantom “_” bug. Playground. I know you can type this in a straight forward way but that’s besides the point for the above reasons. It’s only the case here because this is a minimal repro, in reality the requirements are much more.
Sorry for leaving in the bad comments, good reminder for me for next time 😅