mirror of
https://github.com/zoriya/typebox.git
synced 2026-06-01 18:45:32 +00:00
Recursive Fix: Evaluate Record and Array (#735)
* Fix some infinite recursion with static types * Adds test for #336 * Fix import path * Rename unused type parameter
This commit is contained in:
@@ -26,10 +26,11 @@ THE SOFTWARE.
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
import type { TSchema, SchemaOptions } from '../schema/index'
|
||||
import { CloneType } from '../clone/type'
|
||||
import { Ensure } from '../helpers/index'
|
||||
import type { SchemaOptions, TSchema } from '../schema/index'
|
||||
import type { Static } from '../static/index'
|
||||
import { Kind } from '../symbols/index'
|
||||
import { CloneType } from '../clone/type'
|
||||
|
||||
export interface ArrayOptions extends SchemaOptions {
|
||||
/** The minimum number of items in this array */
|
||||
@@ -45,9 +46,10 @@ export interface ArrayOptions extends SchemaOptions {
|
||||
/** A maximum number of contains schema matches */
|
||||
maxContains?: number
|
||||
}
|
||||
type ArrayStatic<T extends TSchema, P extends unknown[]> = Ensure<Static<T, P>[]>
|
||||
export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptions {
|
||||
[Kind]: 'Array'
|
||||
static: Array<Static<T, this['params']>>
|
||||
static: ArrayStatic<T, this['params']>
|
||||
type: 'array'
|
||||
items: T
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ function FromNumberKey<K extends TNumber, T extends TSchema>(_: K, T: T, options
|
||||
// ------------------------------------------------------------------
|
||||
// prettier-ignore
|
||||
type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (
|
||||
Evaluate<Record<Assert<Static<K>, PropertyKey>, Static<T, P>>>
|
||||
Evaluate<{ [_ in Assert<Static<K>, PropertyKey>]: Static<T, P>; }>
|
||||
)
|
||||
// prettier-ignore
|
||||
export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { Expect } from './assert'
|
||||
import { Type, Static } from '@sinclair/typebox'
|
||||
|
||||
{
|
||||
// identity
|
||||
@@ -78,3 +78,24 @@ import { Type, Static } from '@sinclair/typebox'
|
||||
nodes: Static<typeof T>[]
|
||||
}>()
|
||||
}
|
||||
{
|
||||
// issue: https://github.com/sinclairzx81/typebox/issues/336
|
||||
type JSONValue =
|
||||
| string
|
||||
| number
|
||||
| null
|
||||
| boolean
|
||||
| { [x: string]: JSONValue }
|
||||
| JSONValue[]
|
||||
const R = Type.Recursive((Node) =>
|
||||
Type.Union([
|
||||
Type.Null(),
|
||||
Type.String(),
|
||||
Type.Number(),
|
||||
Type.Boolean(),
|
||||
Type.Record(Type.String(), Node),
|
||||
Type.Array(Node),
|
||||
]),
|
||||
)
|
||||
Expect(R).ToStatic<JSONValue>()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user