Skip to content

Commit

Permalink
feat(core): add const modifier for better typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 2, 2023
1 parent a6b6f4f commit ea118fd
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare global {

type TupleS<X extends readonly any[]> = X extends readonly [infer L, ...infer R] ? [TypeS<L>?, ...TupleS<R>] : any[]
type TupleT<X extends readonly any[]> = X extends readonly [infer L, ...infer R] ? [TypeT<L>?, ...TupleT<R>] : any[]
type ObjectS<X extends Dict> = { [K in keyof X]?: TypeS<X[K]> } & Dict
type ObjectS<X extends Dict> = { [K in keyof X]?: TypeS<X[K]> | null } & Dict
type ObjectT<X extends Dict> = { [K in keyof X]: TypeT<X[K]> } & Dict
type Constructor<T = any> = new (...args: any[]) => T

Expand All @@ -51,10 +51,10 @@ declare global {
is<T>(constructor: Constructor<T>): Schema<T>
array<X>(inner: X): Schema<TypeS<X>[], TypeT<X>[]>
dict<X, Y extends Schema<any, string> = Schema<string>>(inner: X, sKey?: Y): Schema<Dict<TypeS<X>, TypeS<Y>>, Dict<TypeT<X>, TypeT<Y>>>
tuple<X extends readonly any[]>(list: X): Schema<TupleS<X>, TupleT<X>>
object<X extends Dict>(dict: X): Schema<ObjectS<X>, ObjectT<X>>
union<X>(list: readonly X[]): Schema<TypeS<X>, TypeT<X>>
intersect<X>(list: readonly X[]): Schema<IntersectS<X>, IntersectT<X>>
tuple<const X extends readonly any[]>(list: X): Schema<TupleS<X>, TupleT<X>>
object<const X extends Dict>(dict: X): Schema<ObjectS<X>, ObjectT<X>>
union<const X>(list: readonly X[]): Schema<TypeS<X>, TypeT<X>>
intersect<const X>(list: readonly X[]): Schema<IntersectS<X>, IntersectT<X>>
transform<X, T>(inner: X, callback: (value: TypeS<X>) => T, preserve?: boolean): Schema<TypeS<X>, T>
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/tests/intersect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Schema from 'schemastery'

describe('Intersect', () => {
it('intersect (primitive 1)', () => {
const validate = Schema.intersect([String, Number] as const)
const validate = Schema.intersect([String, Number])
expect(validate.toString()).to.equal('string & number')

expect(validate(null)).to.equal(null)
Expand All @@ -14,7 +14,7 @@ describe('Intersect', () => {
})

it('intersect (primitive 2)', () => {
const validate = Schema.intersect([String, 'foo'] as const)
const validate = Schema.intersect([String, 'foo'])
expect(validate.toString()).to.equal('string & "foo"')

expect(validate(null)).to.equal(null)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/tuple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Tuple', () => {
Schema.string().required(),
Schema.number().default(123),
Schema.boolean(),
] as const)
])
expect(Config.toString()).to.equal('[string, number, boolean]')

expect(new Config(['foo'])).to.deep.equal(['foo', 123, undefined])
Expand Down
4 changes: 2 additions & 2 deletions packages/core/tests/union.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Schema from 'schemastery'

describe('Union', () => {
it('primitive', () => {
const config = Schema.union([1, 2] as const)
const config = Schema.union([1, 2])
expect(config.toString()).to.equal('1 | 2')

expect(config(null)).to.equal(null)
Expand All @@ -17,7 +17,7 @@ describe('Union', () => {
const validate = Schema.union([
Schema.object({ a: 'foo', b: Schema.number() }),
Schema.object({ a: 'bar', c: Schema.string() }),
] as const)
])
expect(validate.toString()).to.equal('{ a: "foo", b?: number } | { a: "bar", c?: string }')

expect(validate(null)).to.equal(null)
Expand Down
1 change: 1 addition & 0 deletions packages/form/src/extensions/union.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const config = useModel({
break
}
}
if (deepEqual(value, getFallback(props.schema))) value = null
return value
},
})
Expand Down

0 comments on commit ea118fd

Please sign in to comment.