-
Notifications
You must be signed in to change notification settings - Fork 331
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
Question: Generics and static types #615
Comments
In this case you can not use export interface ConfigValue<T> {
value: T
override: boolean
}
export interface ConfigValueC<TC extends t.Mixed> extends t.Type<TConfigValue<t.TypeOf<T>>, TConfigValue<t.OutputOf<T>>, unknown> {}
export function ConfigValue<TC extends t.Mixed>(codec: TC): ConfigValueC<TC> {
return t.type({
value: codec,
override: boolean
})
} |
Thank you Malte. This helped a lot. Because your answer is not 100% correct, for further reference the final solution I came up with: type ConfigValue<T> = {
value: T;
override: boolean;
};
interface ConfigValueC<TC extends t.Mixed> extends t.Type<ConfigValue<t.TypeOf<TC>>, ConfigValue<t.OutputOf<TC>>, unknown> {}
function createConfigValueT<TC extends t.Mixed>(codec: TC): ConfigValueC<TC> {
return t.type({
value: codec,
override: t.boolean,
});
} The issue can be closed. |
You are welcome Malte 😉 |
@mlegenhausen I have the same issue.
Does it make people define the properties twice? io-ts runtime generic type and TS generic type? |
It's a little hacky but this worked for me: const TConfigValue = <C extends t.Mixed>(codec: C) =>
t.type({
value: codec,
override: t.boolean
});
type ConfigValue<T> = t.TypeOf<ReturnType<typeof TConfigValue<t.Type<T>>>> |
I read the documentation and was not sure how I can extract a generic static type from a generic io-ts type:
I was only able to generate specific static types:
How can I get a static generic type like this:
The text was updated successfully, but these errors were encountered: