Record types should be partial
See original GitHub issueI love that in v3.9 we can now specify the type of the record key using enums or literals.
But the inferred Record
type now needs to be wrapped in Partial
, otherwise typescript forces us to specify every key, which is inconsistent with how the Zod parser interprets the same schema.
For example, suppose we create a schema that allows us to map states to temperatures:
const Weather = z.object({
temperatures: z.record(z.enum(['VT', 'NH', 'CA', 'AZ']), z.enum(['warm', 'cold']))
});
If we map a couple of states, it parses fine. This parses without throwing:
Weather.parse({ temperatures: {VT: 'cold', CA: 'warm'} });
But if we try to use the inferred type with the same object, typescript is not happy:
type Weather = z.infer<typeof Weather>;
const weather: Weather = { temperatures: {VT: 'cold', CA: 'warm'} };
This gives us this error:
Type '{ VT: "cold"; CA: "warm"; }' is missing the following properties from type
'Record<"VT" | "NH" | "CA" | "AZ", "warm" | "cold">': NH, AZ
types.d.ts(639, 124)
It wants us to specify all the states in the enum, which is not intuitive or useful, and is inconsistent with the Zod parser.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Documentation - Utility Types - TypeScript
Partial <Type> Constructs a type with all properties of Type set to optional. This utility will return a type that represents all subsets...
Read more >Define a list of optional keys for Typescript Record
You can create the partial version of your List type: type PartialList = Partial ...
Read more >Flow decision on record type and conditions works partially
The first decision on two different account record types based on the ID seems to only pick the first record type.
Read more >Partial record type? · Issue #429 · gcanti/io-ts - GitHub
A way of cleanly defining a partial record (or a functioning workaround). The closest I've been able to do is t.record(K, t.union([V, t.undefined]))...
Read more >Partial Sandbox: Copy the records that belong to certain ...
Is there a way to copy just the records that belong to certain record types of a standard object? In my case the...
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
@scotttrinh @colinhacks One option might be to accept and document the breaking change as a bug fix for the inconsistency (enum record keys have only been available since 3.9 and are not yet well documented, so won’t be widely adopted). Then we could add a
partial()
method to the record schema so anyone like me who wants the old parsing behavior can still get it.@jeremybull
Ahh, I see what you mean, I missed that part of the issue in my first read. Yeah, we should align the runtime and compile time behavior, I think. @colinhacks this feels like a breaking change since it causes schemas to fail that previously succeeded. Any thoughts or suggestions about what to do here?