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.

Making a struct optional

See original GitHub issue

Hello, if I want to make the author field optional, what do I have to do with this basic piece of code ?

https://github.com/ianstormtaylor/superstruct/blob/07caeb205bffbfc00759381ed84004c0ad4e9c8e/examples/composing-structs.js#L17

const Article = struct({
  id: 'number',
  title: 'string',
  created_at: 'date',
  published_at: 'date?',
  author: User,
})

What I would like to do is just add the question mark sign.

Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
mycaulecommented, Dec 7, 2017

Found the answer in the unit tests, here is the way to do it:

https://github.com/ianstormtaylor/superstruct/blob/master/test/fixtures/object-lists/property-optional.js#L6

struct.optional(User),

or another way

const User = struct.optional({
  id: 'number',
  name: 'string',
})

Maybe you will have to update the docs.

2reactions
ianstormtaylorcommented, Mar 27, 2018

Documented now!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swift Initialize Struct with optional stored properties
I'd like to use Structs to represent a business but I have no idea in advance which combination of properties any specific business...
Read more >
Can Swift Structs have optional stored properties? - Treehouse
Is there a way to define an optional stored property on a swift struct? Something like:
Read more >
Making the most of optionals - Hacking with Swift
One way to solve this is to box the property in a class, then wrap that in an optional. Classes are reference types...
Read more >
Optional field in struct - General Usage - Julia Discourse
It makes it easy define structs with optional keyword arguments, i.e. default values for fields that are not explicitly given. For example b ......
Read more >
solidity - Make a struct member optional when initializing struct
I have a struct where I don't want the value of the address[] arrays to be intialised. But I get a error expected...
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