Skip to content

Commit

Permalink
chore(jex): rm type inference (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
franklevasseur authored Oct 16, 2024
1 parent 3a44a82 commit 74c3214
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 145 deletions.
2 changes: 1 addition & 1 deletion jex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"esbuild": "^0.24.0",
"prettier": "^3.2.4",
"ts-node": "^10.9.2",
"typescript": "5.3.3",
"typescript": "^5.6.3",
"vitest": "1.6.0",
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.4"
Expand Down
14 changes: 7 additions & 7 deletions jex/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion jex/src/jex-representation/from-jex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { expect, test } from 'vitest'
import { $ } from './jex-builder'
import { JexType } from './typings'
import { JSONSchema7 } from 'json-schema'
import { flattenUnions } from './flatten-unions'

const expectJex = (jex: JexType) => ({
toEqualJsonSchema: (expectedJsonSchema: JSONSchema7) => {
Expand Down
52 changes: 2 additions & 50 deletions jex/src/jex-representation/jex-extends.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as utils from '../utils'
import { JexInfer } from './jex-infer'
import * as types from './typings'
import { JexExtensionResult, jexExtends } from './jex-extends'
import { expect, test } from 'vitest'
Expand Down Expand Up @@ -50,29 +48,20 @@ test('jex-extends should be true if child and parent are the same', () => {
test('jex-extends should be true if child is an object with more properties than parent', () => {
const child = $.object({ a: $.string(), b: $.number() })
const parent = $.object({ a: $.string() })
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childExtendsParent = utils.types.Expect<utils.types.Extends<_child, _parent>>
expectJex(child).toExtend(parent)
})

// { a: string } does not extend { a: string, b: number }
test('jex-extends should be false if child is an object with less properties than parent', () => {
const child = $.object({ a: $.string() })
const parent = $.object({ a: $.string(), b: $.number() })
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childNotExtendsParent = utils.types.ExpectNot<utils.types.Extends<_child, _parent>>
expectJex(child).not.toExtend(parent)
})

// { a: string | undefined } does not extend { a: string }
test('jex-extends should be false if an optional property of child is required in parent', () => {
const child = $.object({ a: $.union([$.string(), $.undefined()]) })
const parent = $.object({ a: $.string() })
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childNotExtendsParent = utils.types.ExpectNot<utils.types.Extends<_child, _parent>>
expectJex(child).not.toExtend(parent)
})

Expand All @@ -87,9 +76,6 @@ test('jex-extends should be true if child is an object made of only required pro
test('jex-extends should be true if when child is a union with less types than parent', () => {
const child = $.union([$.string(), $.number()])
const parent = $.union([$.string(), $.number(), $.undefined(), $.null()])
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childExtendsParent = utils.types.Expect<utils.types.Extends<_child, _parent>>
expectJex(child).toExtend(parent)
})

Expand All @@ -104,125 +90,91 @@ test('jex-extends should be false if child is a union with more types than paren
test('jex-extends should be true if child is a literal with same base type than parent', () => {
const child = $.literal('banana')
const parent = $.union([$.string(), $.number()])
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childExtendsParent = utils.types.Expect<utils.types.Extends<_child, _parent>>
expectJex(child).toExtend(parent)
})

// string does not extend "banana"
test('jex-extends should be false if child is a primitive and parent is a literal', () => {
const child = $.string()
const parent = $.literal('banana')
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childNotExtendsParent = utils.types.ExpectNot<utils.types.Extends<_child, _parent>>
expectJex(child).not.toExtend(parent)
})

// "banana" extends string | "apple" | number
test('jex-extends should be true if child is a literal included in parent union', () => {
const child = $.literal('banana')
const parent = $.union([$.string(), $.number(), $.literal('banana')])
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childExtendsParent = utils.types.Expect<utils.types.Extends<_child, _parent>>
expectJex(child).toExtend(parent)
})

// "banana" does not extend "apple" | number
test('jex-extends should be false if child is a literal not included in parent union', () => {
const child = $.literal('banana')
const parent = $.union([$.number(), $.literal('apple')])
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childNotExtendsParent = utils.types.ExpectNot<utils.types.Extends<_child, _parent>>
expectJex(child).not.toExtend(parent)
})

// { a: string }[] extends { a: string | null }[]
test('jex-extends should be true if child and parents are arrays and child items extends parent items', () => {
const child = $.array($.string())
const parent = $.array($.union([$.string(), $.undefined()]))
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childExtendsParent = utils.types.Expect<utils.types.Extends<_child, _parent>>
expectJex(child).toExtend(parent)
})

// { a: string }[] does not extend { a: number }[]
test('jex-extends should be false if child and parents are arrays and child items do not extends parent items', () => {
const child = $.array($.string())
const parent = $.array($.number())
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childNotExtendsParent = utils.types.ExpectNot<utils.types.Extends<_child, _parent>>
expectJex(child).not.toExtend(parent)
})

// [string, string] extends string[], [string, number] extends (string | number)[]
test('jex-extends should be true if child is a tuple and parent is an array with the same items', () => {
const tupleStrStr = $.tuple([$.string(), $.string()])
const arrayStr = $.array($.string())
type _childExtendsParent1 = utils.types.Expect<
utils.types.Extends<JexInfer<typeof tupleStrStr>, JexInfer<typeof arrayStr>>
>
expectJex(tupleStrStr).toExtend(arrayStr)

const tupleStrNum = $.tuple([$.string(), $.number()])
const arrayStrNum = $.array($.union([$.string(), $.number()]))
type _childExtendsParent2 = utils.types.Expect<
utils.types.Extends<JexInfer<typeof tupleStrNum>, JexInfer<typeof arrayStrNum>>
>
expectJex(tupleStrNum).toExtend(arrayStrNum)
})

// [string, string] does not extend number[]
test('jex-extends should be false if child is a tuple and parent is an array with different items', () => {
const child = $.tuple([$.string(), $.number()])
const parent = $.array($.number())
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childNotExtendsParent = utils.types.ExpectNot<utils.types.Extends<_child, _parent>>
expectJex(child).not.toExtend(parent)
})

// { a: string } extends { a: string | number }
test('jex-extends should be true if child and parent are objects and child items extends parent items', () => {
const child = $.object({ a: $.string() })
const parent = $.object({ a: $.union([$.string(), $.number()]) })
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childExtendsParent = utils.types.Expect<utils.types.Extends<_child, _parent>>
expectJex(child).toExtend(parent)
})

// Record<string, string> extends Record<string, string | undefined | boolean>
test('jex-extends should be true if child and parents are maps and child items extends parent items', () => {
const child = $.map($.string())
const parent = $.map($.union([$.string(), $.undefined(), $.boolean()]))
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childExtendsParent = utils.types.Expect<utils.types.Extends<_child, _parent>>
expectJex(child).toExtend(parent)
})

// Record<string, string | undefined | boolean> does not extend Record<string, string>
test('jex-extends should be false if child and parents are maps and child items do not extends parent items', () => {
const child = $.map($.union([$.string(), $.undefined(), $.boolean()]))
const parent = $.map($.string())
type _child = JexInfer<typeof child>
type _parent = JexInfer<typeof parent>
type _childNotExtendsParent = utils.types.ExpectNot<utils.types.Extends<_child, _parent>>
expectJex(child).not.toExtend(parent)
})

// Record<string, string> extends { a?: string }
test('jex-extends should be true if child is a map and parent is object with no required properties', () => {
const child = $.map($.string())
expectJex(child).toExtend($.object({}))
expectJex(child).toExtend($.object({ a: $.union([$.string(), $.undefined()]) }))
})

// Record<string, string> does not extend { a: string }
test('jex-extends should be false if child is a map and parent has required properties', () => {
const child = $.map($.string())
expectJex(child).not.toExtend($.object({ a: $.string() }))
Expand Down
41 changes: 0 additions & 41 deletions jex/src/jex-representation/jex-infer.ts

This file was deleted.

36 changes: 0 additions & 36 deletions jex/src/jex-representation/jex-infer.types.test.ts

This file was deleted.

5 changes: 5 additions & 0 deletions jex/src/jex-representation/property-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export type PropertyPathSection =

export type PropertyPath = PropertyPathSection[]

/**
*
* @param path A data structure that represents a path to a property in a JSON object
* @returns A string representation of the path for easier debugging. This string is used when returning an extension failure reason.
*/
export const pathToString = (path: PropertyPath): string =>
'#' +
path
Expand Down
5 changes: 5 additions & 0 deletions jex/src/jex-representation/to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const _primitiveToString = (jexPrimitive: types.JexPrimitive): string => {
return jexPrimitive.type
}

/**
*
* @param jexSchema the schema to convert
* @returns A string representation of the schema for easier debugging. This string is used when returning an extension failure reason.
*/
export const toString = (jexSchema: types.JexType): string => {
if (jexSchema.type === 'undefined') return 'undefined'
if (jexSchema.type === 'null') return 'null'
Expand Down
2 changes: 1 addition & 1 deletion jex/src/jex-representation/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type JexAny = {

export type JexTuple = {
type: 'tuple'
items: utils.types.Tuple<JexType, number>
items: JexType[]
}

export type JexType =
Expand Down
3 changes: 0 additions & 3 deletions jex/src/utils/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export type Resolve<T> = T extends (...args: infer A) => infer R

export type Cast<T, U> = T extends U ? T : U

type _Tuple<T, N extends number, R extends any[]> = R['length'] extends N ? R : _Tuple<T, N, [T, ...R]>
export type Tuple<T, N extends number> = number extends N ? T[] : _Tuple<T, N, []>

export type Expect<T extends true> = T extends true ? true : 'Expectation failed'
export type ExpectNot<T extends false> = T extends false ? true : 'Expectation failed'

Expand Down
5 changes: 0 additions & 5 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
{
"matchUpdateTypes": ["major"],
"enabled": false
},
{
"matchFileNames": ["jex/**"],
"matchPackageNames": ["typescript"],
"enabled": false
}
]
}

0 comments on commit 74c3214

Please sign in to comment.