Type inference incorrectly sets all fields as optional by default
See original GitHub issueZod 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:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Also be sure to NOT set
"strictNullChecks": false
in yourtsconfig.json
, because this would also result in all properties being infered as optional.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…