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() does not create an optional property on an object

See original GitHub issue

optional does not create an optional property on an object. It should create an optional property in order to match with TypeScript validation semantics.

const CustomText = object({
  name: string()
  age: optional(number())
})

// creates  a type like:
type UserType = {
  name: string
  age: number | undefined
}

// But it should create a type like this:
type UserType = {
  name: string
  age?: number | undefined
}

Alignment with TypeScript

The Superstruct type does not align with the TypeScript type it is returning.

As an example, this passes Superstruct validation:

describe("content-types", () => {
  it("should validate content-types", async () => {
    const User = object({
      name: string(),
      age: optional(number()),
    })
    assert({ name: "john" }, User)
  })
})

but fails the type number | undefined returned by Superstruct:

image

The red squiggles indicate the following error when hovered:

image

Use case

My use case is that I want to validate slate data types.

They are defined such that the bold property is optional. To be specific, they are optional(literal(true). In order to make sure my validations line up against my type definitions exactly, I use unit testing to make sure the two type are equal. Because the optional types show up as a union of the passed in type and undefined but do not add the ? to the property, they do not match and fail tests.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ianstormtaylorcommented, Jun 10, 2020

Ah interesting, that seems like a decent trade off I think. Seems pretty rare that you’d explicitly want a defined undefined value to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Deal with Optional Things and "Undefined" in TypeScript
In strict mode, this means a couple of things. First, if you don't tell TypeScript that a property is optional, it will expect...
Read more >
how to make a property optional but not undefined in typescript
TypeScript does not narrow down the type of x when a new object with a property a that is definitely not undefined is...
Read more >
How to Make Certain Properties Optional in TypeScript
The declaration of the interface is a place to make some properties optional. You can achieve that with a question mark next to...
Read more >
Guide To Java 8 Optional | Baeldung
In this tutorial, we're going to show the Optional class that was introduced in Java 8. The purpose of the class is to...
Read more >
How to use Optional in Java - Developer.com
Absent: The Optional object does represent absence of a value; you cannot access its content with get(). Why Do Developers Need Optional in...
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