Skip to content
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

Explicit types #21

Merged
merged 18 commits into from
Nov 5, 2024
Merged

Explicit types #21

merged 18 commits into from
Nov 5, 2024

Conversation

mrdziuban
Copy link

For a given case class:

case class Foo(int: Int, str: String)

Old generated code

import * as t from "io-ts";

export const fooC = t.type({
  int: t.number,
  str: t.string
});
export type FooC = typeof fooC;
export type Foo = t.TypeOf<FooC>;

New generated code

Comments are included for explanation

import * as t from "io-ts";

// The codec type is generated explicitly and does not refer to the codec value
export type FooC = t.TypeC<{
  int: t.NumberC,
  str: t.StringC
}>;
// The value type is generated explicitly and does not refer to the codec type or value
export type Foo = {
  int: number,
  str: string
};
// The codec value is annotated with the codec type
export const fooC: FooC = t.type({
  int: t.number,
  str: t.string
// The codec value is proven to satisfy `t.Type<ValueType, unknown>`
// This isn't strictly necessary but is nice to verify that the generation of types/values aligns
}) satisfies t.Type<Foo, unknown>;

@mrdziuban mrdziuban merged commit c41b24d into master Nov 5, 2024
6 checks passed
@mrdziuban mrdziuban deleted the explicit-types branch November 5, 2024 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant