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.

Type inference issue for Object schemas

See original GitHub issue

First, Thank you very much for your awesome work 👍

I figured out when I’m using z.infer on an object schema, the inferred type has all the object’s attributes optional. According to the doc, all the attributes should be required.

Example from the doc:

const dogSchema = z.object({
  name: z.string(),
  neutered: z.boolean(),
});

type Dog = z.infer<typeof dogSchema>;

/* 
equivalent to:
type Dog = { 
  name:string; 
  neutered: boolean;
}
*/

But this is what I get:

type Dog = {
    name?: string;
    neutered?: boolean;
}

I’m using zod 1.5.0 and I’ve also tried with 1.3.0 but keep getting the Dog type with optional attributes.

Thanks for your help!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
0radekcommented, May 9, 2020

Thanks so much @vriad

Love this library and the support!

0reactions
colinhackscommented, May 7, 2020

@talmr-k forgot to tag you. Here’s a description on how to set up multiple tsconfigs: https://stackoverflow.com/questions/37579969/how-to-use-multiple-tsconfig-files-in-vs-code

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to infer types of an object created from a schema?
You can do this, using a mapped type, but first you need to preserve the original type of the schema. If you add...
Read more >
Typescript type inference from mongoose Schema · Issue #9715
Given a type Def which represents a schema definition, Entity works by mapping each of its fields into a "non-schema" field (e.g. String...
Read more >
How to Infer Types from Schema : r/typescript - Reddit
So yes, you can start from schema and infer a type for objects that meet this schema, as well as generate a reasonably-typed...
Read more >
Type Inferability and Decidability of the Security Problem ...
The security problem against inference attacks has been formalized under a model of object-oriented databases called method schemas.
Read more >
Algebraic types and schema inference - Chapeau
In particular, schema inference can suffer when you're ingesting a dataset of heterogeneous objects. In this post, I'd like to sketch out ...
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