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

How to extract the static type of mapped output? #143

Open
fernandomrtnz opened this issue Sep 18, 2020 · 1 comment
Open

How to extract the static type of mapped output? #143

fernandomrtnz opened this issue Sep 18, 2020 · 1 comment

Comments

@fernandomrtnz
Copy link

import * as iots from "io-ts";
import * as iotsTypes from "io-ts-types";
import { isRight } from "fp-ts/lib/Either";
import reporter from "io-ts-reporters";

const payload = {
  attributes: {
    description: "desc",
    folderId: "168c3a5e-24c7-467b-8cd6-9324662bcc5e",
    name: "root_folder_1592574328"
  },
  id: "ab1fb10e-a945-4f4d-91d1-b336f8ea7bd9",
  type: "folder-generic"
};

const PayloadCodec = iots.readonly(
  iotsTypes.mapOutput(
    iots.type({
      id: iots.string,
      attributes: iots.type({
        description: iots.string,
        folderId: iots.string,
        name: iots.string
      }),
      type: iots.literal("folder-generic")
    }),
    (o) => ({
      id: o.id,
      type: o.type,
      ...o.attributes
    })
  )
);

// Returns
// {
//   readonly id: string;
//   readonly attributes: {
//       description: string;
//       folderId: string;
//       name: string;
//   };
//   readonly type: "folder";
// }
// but I expected
// {
//   readonly id: string;
//   readonly description: string;
//   readonly folderId: string;
//   readonly name: string;
//   readonly type: "folder";
// }
type Payload = iots.TypeOf<typeof PayloadCodec>;

const decoderResult = PayloadCodec.decode(payload);

if (isRight(decoderResult)) {
  console.log(decoderResult.right);
} else {
  console.log(reporter.report(decoderResult).join("\n"));
}

I expected the Payload type to have the mapped output, but instead I'm receiving the original.

You can play with the example in https://codesandbox.io/s/practical-kalam-9ii7i?file=/src/index.ts.

@gcanti gcanti transferred this issue from gcanti/io-ts Sep 21, 2020
@gcanti
Copy link
Owner

gcanti commented Sep 21, 2020

@fernandomrtnz with mapOutput you can change the output type (i.e. the O type in Type<A, O, I>), looks like you want to change the A type instead

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

No branches or pull requests

2 participants