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.

`optional` is not inferred as optional field in TypeScript types

See original GitHub issue

When 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:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thesunnycommented, Jun 2, 2022

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 or strict mode (which also enables strictNullChecks) it can be easy to forget that strict 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.

0reactions
thesunnycommented, Jun 4, 2022

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 of reference/types.md:

🤖 Warning: You must enable TypeScript’s strictNullChecks option in your tsconfig.json for Superstruct to work properly with “optional” types. Note that strictNullChecks is disabled by default. If you enable strict, strictNullChecks will automatically be enabled.

I think that’s clear enough. I’m closing this issue.

Read more comments on GitHub >

github_iconTop 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 >

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