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

t.TypeOf<keyof<o>> should return a string union type, not a numeric union type. #697

Open
noshiro-pf opened this issue Jun 1, 2023 · 0 comments

Comments

@noshiro-pf
Copy link

noshiro-pf commented Jun 1, 2023

🐛 Bug report

Current Behavior

import * as t from "io-ts";

const T = t.keyof({
  0: undefined,
  1: undefined,
  2: undefined,
  3: undefined,
  4: undefined,
});

type T = t.TypeOf<typeof T>;  // 0 | 1 | 2 | 3 | 4
console.log(T.decode(0));
{
    "_tag": "Left",
    "left": [
        {
            "value": 0,
            "context": [
                {
                    "key": "",
                    "type": {
                        "name": "\"0\" | \"1\" | \"2\" | \"3\" | \"4\"",
                        "keys": {},
                        "_tag": "KeyofType"
                    },
                    "actual": 0
                }
            ]
        }
    ]
}
console.log(T.decode("0"));
{
    "_tag": "Right",
    "right": "0"
}

Expected behavior

import * as t from "io-ts";

const T = t.keyof({
  0: undefined,
  1: undefined,
  2: undefined,
  3: undefined,
  4: undefined,
});

type T = t.TypeOf<typeof T>;  // "0" | "1" | "2" | "3" | "4"

Runtime behavior is ok.

It would be nice if there was some way to support the union of literals trick for numeric literals.

Reproducible example

Suggested solution(s)

Additional context

Your environment

Which versions of io-ts are affected by this issue? Did this work in previous versions of io-ts?

Software Version(s)
io-ts 2.2.20
fp-ts 2.13.1
TypeScript 4.9.3
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

1 participant