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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
t.Type<t.TypeOf<DecoderType>> != DecoderType
for branded types
#576
Comments
this compiles const foo: t.Type<t.TypeOf<typeof Positive>, t.OutputOf<typeof Positive>> = Positive
|
@gcanti Thanks for your explanation! |
I think this is related to this issue. How can I type the import * as t from 'io-ts';
import { UUID } from 'io-ts-types/lib/UUID';
declare function get<T>(key: string, type: t.Type<T>): T;
// Works ok
const stringArray: string[] = get('mykey', t.array(t.string));
// Fails with
// Argument of type 'ArrayC<BrandC<StringC, UUIDBrand>>' is not assignable to parameter of
// type 'Type<Branded<string, UUIDBrand>[], Branded<string, UUIDBrand>[], unknown>'.
const uuidArray: UUID[] = get('mykey', t.array(UUID)); |
There are several ways to do this. declare function get<T>(key: string, type: t.Type<T, any>): T;
declare function get<T extends t.Mixed>(key: string, type: T): t.TypeOf<T>; When you take a look in |
Yes, that's it! Thank you very much |
馃悰 Bug report
Current Behavior
It might not be a bug but a misunderstanding at my part 馃檲 . I in my understanding it should be
t.Type<t.TypeOf<DecoderType>> == DecoderType
. It seems, that this is not true for branded types:In the last line I get the error:
Expected behavior
I can assign the refinement
Positive
tot.Type<t.TypeOf<typeof Positive>>
.Reproducible example
See above
Suggested solution(s)
I have no idea 馃檲
A workaround for is to use the deprecated
t.refinement()
(due to the fact that the old refinements do not carry the refinement to the type level, #373 (comment) ):Additional context
We write a GraphQL API with Apollo ( https://github.com/serlo/api.serlo.org ) where we use decoders to validate JSON responses of different services we use. Internally we derive our types with
t.TypeOf<...>
in order to not have duplicate code. At one place we have a function whose argument is a decoder and where we have something likefunction foo<S extends SomeType>(decoder: t.Type<S>)
. When I wanted to use branded types I came across the above issue.Your environment
Sidenote
Thanks for the great repository which helps us a lot in our ngo work! 馃憤 鉂わ笍
The text was updated successfully, but these errors were encountered: