Skip to content

[GenType] Generated Result type is mismatch with generated js code #6584

Closed
@iamchanii

Description

@iamchanii

Hi. I'm using ReScript v11.0.1 and I wrote some code like these:

[Playground Link]

@genType
let parseEnvironment = hostname =>
  switch hostname->String.split(".")->Array.slice(~start=0, ~end=1) {
  | ["xxx", "alpha"] => Ok(#alpha)
  | ["xxx", _] => Ok(#production)
  | _ => Error({"message": "Failed to parse environment"})
  }

@genType
let makeUrl = hostname =>
  switch parseEnvironment(hostname) {
  | Ok(#production) =>
    Ok("https://foo.com/")
  | Ok(#alpha) =>
    Ok("https://alpha.foo.com/")
  | _ =>
    Error({
      "message": "Failed to make url",
      "fallbackUrl": "https://fallback.foo.com/",
    })
  }
// Generated by ReScript, PLEASE EDIT WITH CARE


function parseEnvironment(hostname) {
  var match = hostname.split(".").slice(0, 1);
  if (match.length !== 2) {
    return {
            TAG: "Error",
            _0: {
              message: "Failed to parse environment"
            }
          };
  }
  var match$1 = match[0];
  if (match$1 !== "xxx") {
    return {
            TAG: "Error",
            _0: {
              message: "Failed to parse environment"
            }
          };
  }
  var match$2 = match[1];
  if (match$2 === "alpha") {
    return {
            TAG: "Ok",
            _0: "alpha"
          };
  } else {
    return {
            TAG: "Ok",
            _0: "production"
          };
  }
}

function makeUrl(hostname) {
  var match = parseEnvironment(hostname);
  if (match.TAG !== "Ok") {
    return {
            TAG: "Error",
            _0: {
              message: "Failed to make url",
              fallbackUrl: "https://fallback.foo.com/"
            }
          };
  }
  var match$1 = match._0;
  if (match$1 === "alpha") {
    return {
            TAG: "Ok",
            _0: "https://alpha.foo.com/"
          };
  } else if (match$1 === "production") {
    return {
            TAG: "Ok",
            _0: "https://foo.com/"
          };
  } else {
    return {
            TAG: "Error",
            _0: {
              message: "Failed to make url",
              fallbackUrl: "https://fallback.foo.com/"
            }
          };
  }
}

export {
  parseEnvironment ,
  makeUrl ,
}
/* No side effect */

And genType result is here: (I cannot found something like playground for genType. this is a edited snippet from my work)

export const parseEnvironment: (hostname:string) => 
    { TAG: "Ok"; _0: "alpha" | "production" }
  | { TAG: "Error"; readonly message: string } = AppContextJS.parseEnvironment as any;

export const makeUrl: (hostname:string) => 
    { TAG: "Ok"; _0: string }
  | { TAG: "Error"; readonly fallbackUrl: string; readonly message: string } = AppContextJS.makeUrl as any;

I think the error tag type should be like { TAG: "Error"; _0: { /* ... */ } }.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions