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 incorrectly sets all fields as optional by default

See original GitHub issue

Zod version: 1.5.0 TS version: 3.8.3

The type inference does not seem to be working properly, making all values of objects to be optional by default.

Let me take this code derived from the readme:

import * as z from "zod";

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

type T = z.TypeOf<typeof dogSchema>;  // -> { name?: string, neutered?: boolean }

const dog: T = {};  // -> Compiles without error

The expected behavior would be a compilation error deriving from name and neutered to be required.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
mkreuzmayrcommented, Jun 15, 2021

Also be sure to NOT set "strictNullChecks": false in your tsconfig.json, because this would also result in all properties being infered as optional.

0reactions
bauti-deficommented, Nov 22, 2022

strict and strictNullChecks both true tsc in the project is 4.8.2 node 16.16.0 zod: 3.19.1

Using VScode. I have restarted the IDE multiple times. All my object field types are still being inferred as optional.


Edit: Seems to be VSCode problem…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type inference incorrectly sets all fields as optional by default
The type inference does not seem to be working properly, making all values of objects to be optional by default. Let me take...
Read more >
swift - Type inference fails when using nil-coalescing operator ...
Our framework contains some code for parsing dictionaries into models and we've hit a problem with optional properties with default values. We ...
Read more >
Incorrect types for optional array params - Making Packs - Coda ...
My Zapier Hooks Pack recently broke for a few hours for something that TypeScript should have caught, but didn't due to incorrect types...
Read more >
Angular Strictly Typed Forms (Complete Guide)
To be more precise, and because all form fields are considered nullable by default, the email and password fields are inferred to have...
Read more >
Documentation - Everyday Types - TypeScript
Much like variable type annotations, you usually don't need a return type annotation because TypeScript will infer the function's return type based on...
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