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

Unexpected type widening when using nested isEnum #9

Open
cmfcmf opened this issue May 24, 2021 · 0 comments
Open

Unexpected type widening when using nested isEnum #9

cmfcmf opened this issue May 24, 2021 · 0 comments

Comments

@cmfcmf
Copy link
Contributor

cmfcmf commented May 24, 2021

The type generated by isEnum behaves unexpectedly when nested inside isObject:

// works as expected:
const simpleEnum = t.isEnum(["a", "b"]); 
// ->
// t.StrictValidator<unknown, "a" | "b">

// unexpected type widening:
const enumInObject = t.isObject({
  enum: t.isEnum(["a", "b"]),
});
// ->
// t.StrictValidator<unknown, ToOptional<{
//   enum: string;
// }>>

Current workarounds:

const workaround1 = t.isObject({
  enum: t.isEnum(["a" as const, "b" as const]),
});
// ->
// t.StrictValidator<unknown, ToOptional<{
//   enum: "a" | "b";
// }>>

const workaround2 = t.isObject({
  enum: t.isOneOf([t.isLiteral("a"), t.isLiteral("b")]),
});
// ->
// t.StrictValidator<unknown, ToOptional<{
//   enum: "a" | "b";
// }>>

Possible workaround, but not yet working due to #7:

const workaround3 = t.isObject({
  enum: t.isEnum(["a", "b"] as const),
});
// -> 
// t.StrictValidator<unknown, ToOptional<{
//   enum: any;
// }>>
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