Skip to content

Typesafe-schema v0.5.0

Latest
Compare
Choose a tag to compare
@Nemikolh Nemikolh released this 08 Sep 13:35
· 11 commits to master since this release

Add support for the new TypeScript compiler option exactOptionalPropertyTypes.
This adds a new construct: ObjWithOptional.

ObjithOptional mark properties from its second argument as optional in the TypeScript sense.
For each prop defined in the second argument, you get prop?: type for them
instead of prop: type | undefined or prop: type.

To avoid confusion, in this release Optional is renamed to MaybeUndefined.

Here is an example of the new ObjWithOptional:

ObjWithOptional({
    a: NUMBER,
}, {
    b: STRING
})
// This schema will typecheck values as:
{
    a: number,
    b?: string
}

// If you want b to possibly be undefined you can do instead:
ObjWithOptional({
    a: NUMBER,
}, {
    b: MaybeUndefined(STRING)
})
// This will typecheck values as:
{
    a: number,
    b?: string | undefined
}

Note that this only works when the new flag is set to true. If the flag is set to false or if you use an older version of TypeScript then in both case the type will be:

{
    a: number,
    b?: string | undefined
}

typesafe-schema does not look whether the flag is set to true or not. This means that even though the compiler won't differentiate between the two cases this library will.