Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created April 4, 2022 18:39
Show Gist options
  • Save mattiamanzati/106d04d108c44e0e8749a58403fa9b28 to your computer and use it in GitHub Desktop.
Save mattiamanzati/106d04d108c44e0e8749a58403fa9b28 to your computer and use it in GitHub Desktop.
/**
* @tsplus type Codec
*/
export interface Codec<A> {
decode: (value: unknown) => E.Either<string, A>;
encode: (value: A) => unknown;
}
/**
* @tsplus implicit
*/
export const implicitStrig: Codec<string> = {
decode: (_) => E.right("" + _),
encode: (_) => _,
};
/**
* @tsplus derive Codec<_, _> 10
*/
export declare function deriveStruct<A>(
...rules: A extends Record<string, any>
? [
{
[K in keyof A]: Codec<A[K]>
}
]
: never
): Codec<A>;
/**
* @tsplus derive Codec<_, _> 10
*/
export declare function deriveLiteralString<A extends string>(
...[value]: [value: A]
): Codec<A>
// esempio
interface GetUserRequest {
_tag: "GetUser"
username: string
}
interface GetUserResponse {
name: string
surname: string
}
type Message<Req, Res> = {
request: Codec<Req>
response: Codec<Res>
}
const codecUser = Derive<Message<GetUserRequest, GetUserResponse>>("explain");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment