question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript widening object properties of Zod enum type to string

See original GitHub issue

If I define enum and use it in the object, the object property still has the type of the enum:

enum A { a = "a" }

const a = { a: A.a }
// type of a is { a: A }

However, if I define the same enum using zod, the type of object property is string and not narrowed to enum anymore.

const A = z.enum(["a"])

const a = { a: A.a }
// type of a is { a: string }

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maxArturocommented, Nov 24, 2022

Hi @elemti , I wonder if you have usecases where you need to define a schema in-line with an object? (such as below)

const b = { x: z.enum([‘A’, ‘B’]).parse(123), }; // type of b is { x: string; }

I agree its surprising that TS will widen the type to string inside an object, I’m curious about that and will look at it… but if you can use z.object() for the entire object then it will work as expected:

  const main = z.object({
    x: z.enum(["A", "B"]),
  });

  type testType = z.infer<typeof main>;
  /**
    type testType = {
      x: "A" | "B";
    }
   */

@akoltun I took a look at the implementation, and except for the way the enums are stored and checked (an object for nativeEnum and an array for Enum) there’s not that much difference - for compatibility as the README says if I took a guess. Would love for someone more knowledgeable to chime in!

0reactions
elemticommented, Nov 24, 2022

hi @maxArturo , usually I run into this problem when trying to use .transform() to enhance an object

const schema = z
  .object({
    x: z.enum(['A', 'B']),
  })
  .transform((it) => {
    // do transformations...
    return {
      ...it,
      // "y" will be dependent on "x" in some way
      y: z.enum(['A', 'B']).parse(it.x),
    };
  });
const enhancedObj = schema.parse({});
/*
  const enhancedObj: {
    y: string;
    x: "A" | "B";
  }
*/

I wonder if there’s any better way to enhance / add new keys to an object instead of doing this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating Zod schema from TS type definitions #53 - GitHub
I'm just beginning to convert a large TS project with many types, ... Zod Object Schema --zod.infer--> TS Types --typescript-json-schema--> ...
Read more >
Untitled
I'm using the term "schema" to broadly refer to any data type/structure, from a simple `string` to a complex nested object. Zod is...
Read more >
How to extend enums in TypeScript - LogRocket Blog
Extending an enum allows you to essentially copy a variable definition and add something extra to it.
Read more >
zod - npm
It's a TypeScript-first schema declaration library with rigorous inferred types, incredible developer experience, and a few killer features ...
Read more >
How to Avoid the Infamous "Cannot read properties of ... - Bitovi
With TypeScript, there are two ways of interpreting null and undefined ... because TypeScript is widening the undefined type to string type.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found