-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Inconsistent inference of generic function signature #57373
Comments
The behavior here is an effect of how we perform inference. The steps are:
In an ideal world we'd perform full unification and realize that So, this is effectively working as intended. |
Hmm... That makes sense why, though it seems like slightly inappropriate behavior. I don't expect full unification, but I do expect In particular, the premature resolution of |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Regarding deferral of conditional type resolution, the 2.8 release notes state
The phrase depends on one or more type variables here refers to type variables that are declared in an outer enclosing scope, such as type parameters of a type alias for the conditional type. It specifically doesn't refer to In the original issue example here, all of the type variables are internal to the conditional type and won't cause deferral. Instead, those type variables have to be resolved though inference, which in TypeScript's case always works in one direction or another. Here, as I explained above, we first infer from the check type to the extends type to produce inferences for |
Gotcha. I imagined that Since inference proceeds from the constraint, I also came up with these examples which should work a bit better with inference: // note: identical definitions
type GenFun2<T> = <S extends T>(x:S)=>S
type GenFun3<T> = <S extends T>(x:S)=>S
type B1 = GenFun2<string> extends GenFun2<string> ? true : false // true
// ^?
// even if incompatible types are given this is true (!)
type B1_1 = GenFun2<string> extends GenFun2<number> ? true : false // true, expected false
// ^?
// expect B2 and B3 to be the same, either unknown or null
type B2 = GenFun2<unknown> extends GenFun2<infer T> ? T : null // unknown
// ^?
type B3 = GenFun2<unknown> extends GenFun3<infer T> ? T : null // unknown
// ^?
// expect B4 and B5 to be the same, either object or null
type B4 = GenFun2<object> extends GenFun2<infer T> ? T : null // object
// ^?
type B5 = GenFun2<object> extends GenFun3<infer T> ? T : null // null
// ^? I'm surprised B5 fails, and I'm thinking that B3 works only by some implementation accident. Edit: added B1_1 which demonstrates (I think) that Maybe this is #53210? |
π Search Terms
infer, generic, function
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/dev/bug-workbench/?ts=5.4.0-dev.20240211#code/C4TwDgpgBA4hB2AxArvKBeKAeAGgGgD4AKADwC4cBKdAnAKDtEigEEBGDWBFNCE4BABMAzlCKkyw4ACcAlvADm1AlLmLKUAPxQZyaGSgAzAIYAbYdAD0lndL11rUJwD1NjcNBYAmTnCSooPgF4ETEJVXklGnlDCGkoAC0NbQSoA3hkU1MoR1QAa3gAewB3eDwoYQALQszBKAAjaAjFBxsXNyZPAGZfbgCgoVFxchi4xOVmpS1EtKgMrJybedNyqprTOsaKmUjWpyhXIA
π» Code
π Actual behavior
π Expected behavior
I expect
A2
andA3
to bestring
, since this is the instantiation of the type variable which satisfies theextends
relation.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: