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.

Field becomes optional when value is any or unknown

See original GitHub issue
const s = z.object({ x: z.unknown() })
type s = z.infer<typeof s>
// s has { x?: unknown } but should be { x: unknown }

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Dec 5, 2022

How does one write anything but something must be passed in and it cannot be undefined?

This is the best I’ve got:

const schema = z.object({
    field: z.custom((x) => x !== undefined),
  });
1reaction
lostpebblecommented, Dec 13, 2022

What does it infer to? I’m almost certain it will infer to {}, which is the same as unknown and this will not correctly resemble empty objects. Actually, everything that extends an object will pass this from the inference point of view.

It infers to object - which is a standard TypeScript type for object and makes sense in this situation.

These type assertions seem to be correct to me: image

In the code, I’ve made sure the “output” type is object in this case- so yea, its not quite the same as passing in {} and making it "passthrough"- there was a little more involved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional chaining of unknown should be unknown #37700
But for all possible types in JavaScript, optional chaining will return undefined if it does not apply or cannot find the field.
Read more >
When to use `never` and `unknown` in TypeScript
Any value can be assigned to a variable of type unknown . This means that unknown is a supertype of every other type....
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
This results in shorter and simpler expressions when accessing chained properties when the possibility exists that a reference may be missing.
Read more >
“Three way optionals” / Distinguishing unknown and ...
In some cases, it is important to distinguish between “unknown value” (because it has not been allocated; because it is not determinable ...
Read more >
Why type is being set to unknown on optional field in ...
The address property has unknown type for some reason. When I use syntax like man1 or man3 everything is working, you can replace...
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