-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Conditional types infer the wrong type for generic constraints #41677
Comments
This looks like a duplicate of #39736. |
I disagree—that issue is described as an abbreviation syntax, which this is not. Furthermore, it’s not at all clear that type ConstraintOf<Fn extends <S extends infer Constraint>(arg: S) => any> = Constraint; would work any better in the world where #39736 has been implemented than my own variants above do here. |
krryan
changed the title
Conditional types cannot extract constraints on generics
Conditional types infer the wrong type for generic constraints
Nov 25, 2020
On the other hand, it is a duplicate of #41040. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: 4.2.0-dev.20201115
Search Terms: conditional type generic constraint
Code
As a variant, also tried
and
and
None of these variants changed the observed behavior.
Expected behavior:
The type of
Test
is{ foo: 'bar' }
.Actual behavior:
The type of
Test
isunknown
. This is particularly problematic because the constraint on the function implies a minimum requirement for safely calling it, which is lost when the type becomesunknown
and thus it would compile without error for any value passed in.Playground Link:
Unavailable: in the playground,
<S extends { foo: 'bar' }>(arg: S) => 'baz'
cannot be passed toConstraintOf
as it is not considered to extend<S>(arg: S) => any
in the first place. Removing the constraint onConstraintOf
itself yieldsnever
as it just fails theextends
check in the conditional type.Related Issues: None I’m aware of.
Purpose:
My goal here is to be able to take a generic function (known to be of the form
<S extends Foo>(arg: S) => Bar
, whereFoo
andBar
can vary, and be able construct a new function that takes the same argument, with the same constraint, but returns something else.Specifically, I am working on a
compose
function that can handle (this one specific case of) generic functions, i.e.Generalizing this requires higher-order kinds, but it seems to me that this specific use-case should not. I just want to extract the constraint (known to be that one single value), and use it in another type.
This almost works:
Then
test
has the type<S extends unknown>(arg: S) => S & { bar: 3; } & { baz: 'test' }
.The text was updated successfully, but these errors were encountered: