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

Rework Record and Tagged Union API #17

Merged
merged 4 commits into from
Apr 14, 2022

Conversation

harrysolovay
Copy link
Contributor

@harrysolovay harrysolovay commented Apr 13, 2022

Records

const c = s.record(
  ["name", s.str],
  ["nickName", s.str],
  ["superPower", s.option(s.str)],
  ["luckyNumber", s.u8],
);

... encodes from / decodes to

interface C {
  name: string;
  nickName: string;
  superPower: string | undefined;
  luckyNumber: number;
} 

Tagged Unions

const c = s.taggedUnion(
  ["A"],
  ["B", ["B", s.str]],
  ["C", ["C", s.tuple(s.u32, s.u64)]],
  ["D", ["D", s.record(
    ["a", s.u32],
    ["b", s.u64],
  )]],
);

... encodes from / decodes to

type C = {
  _tag: "A";
} | {
  _tag: "B";
  B: string;
} | {
  _tag: "C";
  C: [number, bigint];
} | {
  _tag: "D";
  D: {
    a: number;
    b: bigint;
  }
}

@harrysolovay harrysolovay merged commit 18b736e into main Apr 14, 2022
@harrysolovay harrysolovay deleted the rework-record-and-tagged-union-api branch April 14, 2022 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant