-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Generic function constraints degrade in conditionas but work in higher order functions #61364
Comments
It's a current design limitation. A conditional type like this can't produce a generic function, type parameters are erased and replaced with what got inferred for them (and that can be their constraints, like declare const wrapReturnB: WrapReturnB<typeof someFn>
// ^? const wrapReturnB: (P1: unknown, p2: unknown) => { wraps: [unknown, unknown]; } |
Thanks for the clarification @Andarist. Is there any other way how to derive a type from a generic function (to tweak its signature) and keep the constraints? |
You already have an example of that in your playground. A runtime-level call can produce a new "adapted" generic signature and you are already returning one from this call: |
Yeah I know, but I meant purely on the type level |
I don't think it's possible today. |
π Search Terms
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=4.0.5#code/PTAEAUCcFMGdsgNzqAxgewHawC6QIYCWmOso6AZqBZgLABQOAngA7SgDqBLAStDgFdImAIKgAvKAA8Y6AA8c0TABMyAzAGtM6AO6YA2gF0ANKB4A+ABQ0AXKEsA6J-kgBzWHZEBKCebM-xcwZQeycHF3dPAL8Abx1uDzMAXwYGEFAAEWhXAmUUDGw8IhIySlAAFVAcdFB1LV06RlZ2LnxefiFMACEpcr9JSvlFFTJHZzdE4goEUG9fUCmZnmCQ0AB+UPHI2ejQOIS7HhT6VdA7TGhkSFT6PNQAGxd2AtxQWHQAW2gAMUw7KXAAEZTOAAExWIF2IGmFigqGg3b6aEQUGGG53R4wNBYV7xNp8QTCER2VrtQmiBgYp7YwqgPFkzpdEncAmMqTMNhld5fX5BeiU6APakvHCgOR2ADk+AARqgJQKhViRaAmHZAaCAMw3dIAYRxRWIpHIVG5P0woGpLBg8CQ0GUDGV+AkdJZHSJllNvy8ljkpiYPnS+zaiX0UtlEtM6o1hmOjqdknprPdibdog9nzNXm9vpVAbAQZYiQLIbDcsjmpjxzSYD1hQIhtKJozv1AeRy+DyyiqNTq2j0Dv1oGlzpT5K6Pr9eb29JDvYapjnehjQA
π» Code
π Actual behavior
Wrapping (intercepting/decorating) a function (to for example wrap the return value) with generic constraints by inferring its arguments and return value in a generic type degrades the function's constraints to
unknown
:The constraints are however preserved and propagated if the type is a generic (higher order) function:
π Expected behavior
It would be great if the generic constraints of a function wrapped with inference in a conditional type would not be degraded to
unknown
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: