`optional` is not inferred as optional field in TypeScript types
See original GitHub issueWhen using object
with an optional field, that field is not optional in the inferred TypeScript type:
const TestStruct = object({
foo: optional(string())
});
type Type = Infer<typeof TestStruct>; // { to: string }
I would expect the type to be inferred as { to?: string }
instead.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:6 (2 by maintainers)
Top Results From Across the Web
How can I infer optional properties in TypeScript?
Although the optional field on the userSchema above is inferred as a boolean , it's possible to infer it as a boolean literal...
Read more >Documentation - Advanced Types - TypeScript
From TypeScript 3.7 and onwards, you can use optional chaining to simplify working with nullable types.
Read more >Documentation - Classes - TypeScript
It is not - implements clauses don't change how the class body is checked or its type inferred. Similarly, implementing an interface with...
Read more >Handbook - Interfaces - TypeScript
Not all properties of an interface may be required. Some exist under certain conditions or may not be there at all. These optional...
Read more >Documentation - Object Types - TypeScript
In those cases, we can mark those properties as optional by adding a question ... Property 'name' of type 'string' is not assignable...
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
Hi @ianstormtaylor
My comment was ambiguous so to clarify…
I mean to suggest to add to the documentation that SuperStruct won’t work specifically with the default tsconfig. In other words, one must manually enable the option as a step on a new project. Because a lot of presets enable
strictNullChecks
orstrict
mode (which also enablesstrictNullChecks
) it can be easy to forget thatstrict
mode is not on by default.Any chance to give me contributor access to this project? I know I can do a PR but I find I contribute more often when I can just pull and push.
Hi @ianstormtaylor
I like that wording. I added a few details. Here’s what I ended up with which I updated and also added to the
optional
section ofreference/types.md
:I think that’s clear enough. I’m closing this issue.