superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

optional on unknown is still required, probably a bug?

Open teneon opened this issue 3 years ago • 1 comments

Hi there,

there seems to be a bug when you try to make unknown() optional. Let's take a look at the simple example:

  const TestStruct = object({
    foo: optional(string()),
    baz: optional(unknown())
  });

  type Type = Infer<typeof TestStruct>;

Both foo and baz should be optional, but if you add the snippet to VSCode, hover mouse over Type, you can see that "foo" is optional, but "baz" is required.

If we compare this to the standard TS type:

  type Type = {
    baz?: unknown
  }

If you hover mouse over "Type", you can see that "baz" is actually optional, which is also expected behaviour. It looks like a bug to me? Please let me know what u guys think?

Amazing library btw! ;)

best regards, Neon

teneon avatar Jul 06 '21 07:07 teneon

It seems like this is a limitation in TypeScript itself?

type A = unknown;
type B = unknown | undefined;

Both are inferred as unknown by TypeScript.

Mrtenz avatar Sep 26 '22 15:09 Mrtenz