Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclude never from union's types #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/__tests__/union-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {expect, test} from 'vitest'

import {literal} from '../creators/literal'
import {never} from '../creators/never'
import {number} from '../creators/number'
import {object} from '../creators/object'
import {string} from '../creators/string'
import {union} from '../creators/union'

test('typed object union excludes never types', () => {
const unionSchema = union([
object({
_type: literal('ok'),
name: string(),
}),
never(),
])
const neverType = unionSchema.union.find(schema => {
// @ts-expect-error - should not be in type system
return schema.typeName === 'never'
})
expect(neverType).toBeUndefined()
})
test('primitive union excludes never types', () => {
const unionSchema = union([string(), number(), never()])
const neverType = unionSchema.union.find(schema => {
// @ts-expect-error - should not be in type system
return schema.typeName === 'never'
})
expect(neverType).toBeUndefined()
})
10 changes: 9 additions & 1 deletion src/__tests__/union.types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {test} from 'vitest'
import {expectTypeOf, test} from 'vitest'

import {boolean} from '../creators/boolean'
import {literal} from '../creators/literal'
Expand All @@ -7,6 +7,8 @@ import {number} from '../creators/number'
import {object} from '../creators/object'
import {string} from '../creators/string'
import {union} from '../creators/union'
import {type SanityNever, type SanityObjectUnion} from '../defs'
import {type ElementType} from '../helpers/utilTypes'

test('primitive unions', () => {
union([string(), union([number(), boolean()])])
Expand All @@ -27,3 +29,9 @@ test('object union of object union', () => {
union([object({_type: literal('baz'), foo: string()}), never()]),
])
})

test('union type excludes never type', () => {
expectTypeOf<ElementType<SanityObjectUnion['union']>>()
.extract<SanityNever>()
.toEqualTypeOf<never>()
})
7 changes: 5 additions & 2 deletions src/creators/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ export function union<
isPrimitiveUnionSchema(schema),
)
) {
return defineType({typeName: 'primitiveUnion', union: unionTypes}) as any
return defineType({
typeName: 'primitiveUnion',
union: unionTypes.filter(schema => !isNeverSchema(schema)),
}) as any
}

if (
Expand All @@ -109,7 +112,7 @@ export function union<
) {
return defineType({
typeName: 'union',
union: unionTypes,
union: unionTypes.filter(schema => !isNeverSchema(schema)),
}) as any
}

Expand Down
4 changes: 2 additions & 2 deletions src/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface SanityObjectUnion<
Output = OutputOf<Def>,
> extends SanityType<Output> {
typeName: 'union'
union: Def[]
union: Exclude<Def, SanityNever>[]
}
export interface SanityPrimitiveUnion<
Def extends SanityPrimitive | SanityLiteral | SanityNever =
Expand All @@ -94,7 +94,7 @@ export interface SanityPrimitiveUnion<
Output = OutputOf<Def>,
> extends SanityType<Output> {
typeName: 'primitiveUnion'
union: Def[]
union: Exclude<Def, SanityNever>[]
}

export type SanityObjectShape = {[key: string]: SanityAny}
Expand Down