Skip to content

Commit

Permalink
Fix assertions of union types
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Aug 10, 2018
1 parent 20b299d commit 45b9708
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions test/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ const personValidator = createValidator({

if (personValidator(input)) {
assertType<string>(input.name)
assertType<number | undefined>(input.age)
input.age === undefined
input.age === 1
if (typeof input.age !== 'undefined') assertType<number>(input.age)
if (typeof input.age !== 'number') assertType<undefined>(input.age)
}

const namesValidator = createValidator({
Expand Down Expand Up @@ -139,7 +138,8 @@ const user2Validator = createValidator({

if (user2Validator(input)) {
assertType<{ first: string | undefined, last: string }>(input.name)
assertType<string | undefined>(input.name.first)
if (typeof input.name.first !== 'undefined') assertType<string>(input.name.first)
if (typeof input.name.first !== 'string') assertType<undefined>(input.name.first)
assertType<string>(input.name.last)

if (input.items !== undefined) {
Expand All @@ -165,7 +165,9 @@ const specificValuesValidator = createValidator({
})

if (specificValuesValidator(input)) {
assertType<true | 1000 | 'XX'>(input)
if (input !== true && input !== 1000) assertType<'XX'>(input)
if (input !== 1000 && input !== 'XX') assertType<true>(input)
if (input !== 'XX' && input !== true) assertType<1000>(input)
}

const metricValidator = createValidator({
Expand Down

0 comments on commit 45b9708

Please sign in to comment.