gigobyte / purify

Functional programming library for TypeScript - https://gigobyte.github.io/purify/
ISC License
1.5k stars 57 forks source link

Codec with optional `unkown` has unexpected type `unknown` instead of `unknown | undefined` #712

Closed emiel closed 3 days ago

emiel commented 4 days ago

First off, thanks for the great lib! ;)

In the following codec definition, I'm expecting Foo.foo to be of type unknown | undefined just like Foo.bar is. Is there something special about unknown or is this a bug?

import * as pur from "purify-ts";

export const fooCodec = pur.Codec.interface({
  foo: pur.optional(pur.unknown),
  bar: pur.optional(pur.boolean),
});

export type Foo = pur.GetType<typeof fooCodec>;
image
$ tsc --version
Version 5.5.2
gigobyte commented 4 days ago

That is a feature of TypeScript, it's the same with any | undefined. Because unknown includes undefined in itself it doesn't make a lot of sense to keep it in the definition.

image
emiel commented 3 days ago

Of course, that makes sense. Given any and unknown are TypeScript's Top type.

Thanks!