Skip to content

Commit

Permalink
feat: try another approach
Browse files Browse the repository at this point in the history
  • Loading branch information
iamandrewluca committed Oct 11, 2024
1 parent 2f3f1fe commit c5bd7a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions source/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const userMaskSettings: UserMask = {
@category Object
*/
export type Schema<ObjectType, ValueType> = ObjectType extends string
export type Schema<ObjectType, ValueType, Options extends SchemaOptions = {}> = ObjectType extends string
? ValueType
: ObjectType extends Map<unknown, unknown>
? ValueType
Expand All @@ -48,7 +48,9 @@ export type Schema<ObjectType, ValueType> = ObjectType extends string
: ObjectType extends ReadonlySet<unknown>
? ValueType
: ObjectType extends Array<infer U>
? Array<Schema<U, ValueType>>
? Options['recurseIntoArrays'] extends true
? Array<Schema<U, ValueType>>
: ValueType
: ObjectType extends (...arguments_: unknown[]) => unknown
? ValueType
: ObjectType extends Date
Expand All @@ -69,3 +71,13 @@ type SchemaObject<ObjectType extends object, K> = {
? Schema<ObjectType[KeyType], K>
: Schema<ObjectType[KeyType], K> | K;
};

/**
@see Schema
*/
export type SchemaOptions = {
/**
@default true
*/
readonly recurseIntoArrays?: boolean;
};
2 changes: 1 addition & 1 deletion test-d/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const fooSchema: FooSchema = {
symbol: 'A',
map: 'A',
set: 'A',
array: ['A'],
array: 'A',
tuple: ['A'],

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.2.0

Type 'string[]' is not assignable to type 'FooOption'.

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.3.0

Type 'string[]' is not assignable to type 'FooOption'.

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript latest

Type 'string[]' is not assignable to type 'FooOption'.

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Type 'string[]' is not assignable to type 'FooOption'.

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.1.0

Type 'string[]' is not assignable to type 'FooOption'.

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Type 'string[]' is not assignable to type 'FooOption'.

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Type 'string[]' is not assignable to type 'FooOption'.

Check failure on line 41 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.4.0

Type 'string[]' is not assignable to type 'FooOption'.
objectArray: [{key: 'A'}],

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.2.0

Type '{ key: string; }' is not assignable to type 'string'.

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.3.0

Type '{ key: string; }' is not assignable to type 'string'.

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript latest

Type '{ key: string; }' is not assignable to type 'string'.

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Type '{ key: string; }' is not assignable to type 'string'.

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.1.0

Type '{ key: string; }' is not assignable to type 'string'.

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Type '{ key: string; }' is not assignable to type 'string'.

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Type '{ key: string; }' is not assignable to type 'string'.

Check failure on line 42 in test-d/schema.ts

View workflow job for this annotation

GitHub Actions / TypeScript ~5.4.0

Type '{ key: string; }' is not assignable to type 'string'.
readonlyMap: 'A',
Expand Down

0 comments on commit c5bd7a6

Please sign in to comment.