Field becomes optional when value is any or unknown
See original GitHub issueconst s = z.object({ x: z.unknown() })
type s = z.infer<typeof s>
// s has { x?: unknown } but should be { x: unknown }
Issue Analytics
- State:
- Created 10 months ago
- Comments:12 (6 by maintainers)
Top 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 >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
This is the best I’ve got:
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:
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.