-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tuple spreading * more dx tests * some changes * latest * cleanup * decided to drop implementation of rest tuples * update build outputs
- Loading branch information
1 parent
63449f4
commit 0a4fb87
Showing
12 changed files
with
541 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
import { Sure, InferBad, InferGood, MetaNever, MetaObj } from './core.js'; | ||
export declare function array<TPropFail, TPropGood, TSchema extends Sure<TPropFail, TPropGood, unknown, MetaObj | MetaNever>>(schema: TSchema): Sure<Array<InferBad<TSchema> | undefined>, Array<InferGood<TSchema>>, unknown, MetaObj<TSchema>>; | ||
export declare function array<TPropFail, TPropGood, TSchema extends Sure<TPropFail, TPropGood, unknown, MetaObj | MetaNever>>(schema: TSchema): Sure<Array<InferBad<TSchema> | undefined>, Array<InferGood<TSchema>>, unknown, MetaObj<{ | ||
parent: typeof array; | ||
schema: TSchema; | ||
}>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,34 @@ | ||
import { MetaNever, MetaObj, Sure } from './core.js'; | ||
export type TupleInferGoods<T> = T extends readonly [infer First, ...infer InferRest] ? First extends Sure<unknown, infer Good, any, MetaObj | MetaNever> ? [Good, ...TupleInferGoods<InferRest>] : [] : []; | ||
export type TupleInferBads<T> = T extends readonly [infer First, ...infer InferRest] ? First extends Sure<infer Bad, unknown, any, MetaObj | MetaNever> ? [Bad | undefined, ...TupleInferBads<InferRest>] : [] : []; | ||
export declare function tupleRest<Arr extends Sure<unknown, unknown[], unknown>>(struct: Arr): Sure<unknown, unknown[], unknown, MetaObj<{ | ||
func: typeof tupleRest; | ||
initial: unknown; | ||
import { InferBad, InferGood, MetaNever, MetaObj, Sure } from './core.js'; | ||
export type TupleInferGoods_old<T> = T extends readonly [infer First, ...infer InferRest] ? First extends Sure<unknown, infer Good, any, MetaObj | MetaNever> ? [Good, ...TupleInferGoods<InferRest>] : [] : []; | ||
export type TupleInferGoods<T> = T extends readonly [infer First, ...infer InferRest] ? First extends Sure<unknown, infer Good, any, infer Meta> ? Meta extends MetaObj<{ | ||
parent: typeof spread_NOT_IMPLEMENTED; | ||
}> ? Good extends readonly unknown[] ? [...Good, ...TupleInferGoods<InferRest>] : [ | ||
Good, | ||
...TupleInferGoods<InferRest> | ||
] : [Good, ...TupleInferGoods<InferRest>] : [] : []; | ||
export type TupleInferBads_old<T> = T extends readonly [infer First, ...infer InferRest] ? First extends Sure<infer Bad, unknown, any, MetaObj | MetaNever> ? [Bad | undefined, ...TupleInferBads<InferRest>] : [] : []; | ||
export type TupleInferBads<T> = T extends readonly [infer First, ...infer InferRest] ? First extends Sure<infer Bad, unknown, any, infer Meta> ? Meta extends MetaObj<{ | ||
parent: typeof spread_NOT_IMPLEMENTED; | ||
}> ? Bad extends readonly unknown[] ? [...Bad, ...TupleInferBads<InferRest>] : [ | ||
Bad | undefined, | ||
...TupleInferBads<InferRest> | ||
] : [Bad | undefined, ...TupleInferBads<InferRest>] : [] : []; | ||
/** | ||
* @deprecated | ||
* | ||
* It's possible to add a spread operator. | ||
* But it's seems that it would add a lot of complexity and will have to link the library too much. | ||
* | ||
* A tuple would have to know about both the spread and an array. | ||
* | ||
* It made sense for `object` and `optional`, since they're linked. | ||
* But for tuples it can be more clearly implemented as a separate user-land function. | ||
*/ | ||
export declare function spread_NOT_IMPLEMENTED<Arr extends Sure<unknown, unknown[], unknown>>(schema: Arr): Sure<InferBad<Arr>, InferGood<Arr>, unknown, MetaObj<{ | ||
parent: typeof spread_NOT_IMPLEMENTED; | ||
schema: typeof schema; | ||
}>>; | ||
export declare function tuple<Arr extends [Sure<unknown, unknown, any>, ...Sure<unknown, unknown, any>[]] | []>(arr: Arr): Sure<TupleInferBads<Arr>, TupleInferGoods<Arr>, unknown, MetaObj<{ | ||
parent: typeof tuple; | ||
schema: Arr; | ||
}>>; | ||
export declare function tuple<Arr extends [Sure<unknown, unknown, any>, ...Sure<unknown, unknown, any>[]] | []>(arr: Arr): Sure<TupleInferBads<Arr>, TupleInferGoods<Arr>, unknown, MetaObj<Arr>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { MetaObj, Sure, array, number, spread_NOT_IMPLEMENTED, string, tuple } from '../../index.js' | ||
|
||
// Doesn't work | ||
export type TupleInferGoods01<T> = // | ||
T extends readonly [infer First, ...infer InferRest] | ||
? First extends Sure<unknown, infer Good, any, infer Meta> | ||
? Meta extends { parent: typeof spread_NOT_IMPLEMENTED } | ||
? // try spreading the rest if it's an array | ||
Good extends readonly unknown[] | ||
? [...Good, ...TupleInferGoods01<InferRest>] | ||
: // otherwise just return the good | ||
[Good, ...TupleInferGoods01<InferRest>] | ||
: [Good, ...TupleInferGoods01<InferRest>] | ||
: [] | ||
: [] | ||
|
||
const sample = [ | ||
// | ||
string, | ||
spread_NOT_IMPLEMENTED(array(number)), | ||
string, | ||
] as const | ||
|
||
type Sample = typeof sample | ||
|
||
// [unknown[]] | ||
type Check01 = TupleInferGoods01<Sample> | ||
|
||
// ## Try fixing | ||
export type TupleInferGoods_02<T> = // | ||
T extends readonly [infer First, ...infer InferRest] | ||
? First extends Sure<unknown, infer Good, any, infer Meta> | ||
? Meta extends MetaObj<{ parent: typeof spread_NOT_IMPLEMENTED }> | ||
? // try spreading the rest if it's an array | ||
Good extends readonly unknown[] | ||
? [...Good, ...TupleInferGoods_02<InferRest>] | ||
: // otherwise just return the good | ||
[Good, ...TupleInferGoods_02<InferRest>] | ||
: [Good, ...TupleInferGoods_02<InferRest>] | ||
: [] | ||
: [] | ||
|
||
// works | ||
type Check02 = TupleInferGoods_02<Sample> | ||
|
||
// Check for full tuple | ||
const schema = [ | ||
// | ||
string, | ||
spread_NOT_IMPLEMENTED(array(number)), | ||
string, | ||
] as const |
Oops, something went wrong.