Skip to content
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

The generic type cannot be successfully inferred. #57185

Closed
LiST-GIT opened this issue Jan 26, 2024 · 2 comments
Closed

The generic type cannot be successfully inferred. #57185

LiST-GIT opened this issue Jan 26, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@LiST-GIT
Copy link

πŸ”Ž Search Terms

The generic type cannot be successfully inferred.

πŸ•— Version & Regression Information

5.3

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBMEM7APoCMA8AVANFAwugGgHxQC8U6UEAHrAHYAmcUA3gL5QD8LUA2gNJQAlrSgBrCCAD2AM3IBdAFzl+cyjQgMmACgDGwKkoCGtENklhgcIyewA6e4YBOAcytDa0iI6gAFAJSkxMKe3gBKnFC6+kp4+GYWbsxioqJKtACuALbIXlCsdg4ubv6BUOFpEABuuexKAEwA3ABQ0um0eoKSIoY9hqgAItjohFrASkn0HEqDUNJT5FAAPjDwSGhY3HBbaVk53qzErAFJrE29hlrMTVA3UAD0d1D041BbbgCMdQDMedgPuJJMpkNMBhM4YAALQRMAA2wmgknSwCgAHdBDCYVAQIIIDD6E9BNIQiCoI54OkYZZbNdbtJxjTbrcLnoqPFLNhescGYyeSzbG9uYz8oLbsgoqyoOZ2VBOSwRTyoHyBQqbsKVYrxWy4Byelz1YylVt5b9jW8lAByAACCCcwHNmG5wqOQA

πŸ’» Code

type test_b<T, CTX> = T extends {} ? { [K in keyof T]: T[K] extends (ctx: any, opts: any, ...args: infer P) => infer R ? (ctx: CTX, opts: { kkk: number }, ...args: P) => R : never } : 2;
function aaaa<D, T>(t: { d?: D, f?: T | test_b<T, { sss: number }> }) { }
aaaa({
    // d: { sss: 123 }, // Commenting this line out will yield different results.
    f: {
        a(ctx, opts, aaa) {
            ctx.sss
        },
        b(ctx, opts, aaa) {
            ctx.sss
        },
        c(ctx, opts, aaa) {
            ctx.sss
        },
        sss: '@start',
    },
})

πŸ™ Actual behavior

The error occurred after set d.

πŸ™‚ Expected behavior

It should always converge successfully.

Additional information about the issue

No response

@Andarist
Copy link
Contributor

This is an interesting example of #47599

The problem here is that when u add ur d property you make the whole object "partially inferrable" and the inference algorithm successfully infers { sss: number; }. At the same time, the other part of the object is non-inferrable (due to the presence of context-sensitive functions). Judging by the observable results the parameter types are pulled before inferences for this reverse mapped type (T) can be made (maybe before a candidate for that T can even be created).

When the property is not included no inferences are made in the first pass~ and the reverse mapped type and TS avoids pulling the parameter types. In the second pass~ the candidate for T can finally be created and it resolves much better in your case.

Or something like that πŸ˜‰

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants