-
Notifications
You must be signed in to change notification settings - Fork 330
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
Incorrect type
resulting type when contains refinement
#680
Comments
I think it has something to do with the output type, as I could fix my error by forcing it to import * as io from 'io-ts';
export type Brand<T, N> = T & { __brand: N };
export type SensorId = Brand<string, 'SensorId'>;
interface SomeInterface {
id: SensorId;
}
const InterfaceCodec = io.type({
id: io.refinement(io.string, (id: string): id is SensorId => true) as io.Type<
SensorId,
unknown // <- here
>,
});
const codec: io.Type<SomeInterface, unknown> /* <- and here */ = InterfaceCodec; How can I avoid doing that ? Why is |
You want to define a output interface interface SomeInterfaceOutput {
id: string;
} The const codec: io.Type<SomeInterface, SomeInterfaceOutput> = InterfaceCodec; When you now |
Thank you for your answer. Oh, OK, I understand that this is just because output type parameter of Actually, I don’t care about the output type interface, I just wanted to validate that my Codec correspond to an existing type, so I wanted to infer it in some way … I just found out I can simply use the export type Brand<T, N> = T & { __brand: N };
export type SensorId = Brand<string, 'SensorId'>;
interface SomeInterface {
id: SensorId;
}
const InterfaceCodec = io.type({
id: io.refinement(io.string, (id: string): id is SensorId => true),
});
const codec: io.Type<SomeInterface, io.OutputOf<typeof InterfaceCodec>> = InterfaceCodec; Anyway, thanks for you answer, and thanks for the work of this lib ! |
🐛 Bug report
Current Behavior
This generates typing error TS2322 :
Expected behavior
I guess that
type
withrefinement
should generate aType
that is compatible with the branded type, not just string. So I don’t think we should have an error here … or am I mistaken ?Your environment
The text was updated successfully, but these errors were encountered: