You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
None of these variants changed the observed behavior.
Expected behavior:
The type of Test is { foo: 'bar' }.
Actual behavior:
The type of Test is unknown. This is particularly problematic because the constraint on the function implies a minimum requirement for safely calling it, which is lost when the type becomes unknown 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 to ConstraintOf as it is not considered to extend <S>(arg: S) => any in the first place. Removing the constraint on ConstraintOf itself yields never as it just fails the extends 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, where Foo and Bar 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.
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
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: