@strictly/define 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +31 -0
- package/.out/index.d.ts +20 -0
- package/.out/index.js +20 -0
- package/.out/transformers/copies/copy.d.ts +4 -0
- package/.out/transformers/copies/copy.js +7 -0
- package/.out/transformers/copies/copy_to.d.ts +6 -0
- package/.out/transformers/copies/copy_to.js +91 -0
- package/.out/transformers/copies/mobx_copy.d.ts +5 -0
- package/.out/transformers/copies/mobx_copy.js +42 -0
- package/.out/transformers/copies/specs/copy_to.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/copy_to.tests.js +97 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.d.ts +1 -0
- package/.out/transformers/copies/specs/mobx_copy.tests.js +19 -0
- package/.out/transformers/flatteners/flatten_accessors_of.d.ts +5 -0
- package/.out/transformers/flatteners/flatten_accessors_of.js +11 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_json_value_to_type_paths_of.js +10 -0
- package/.out/transformers/flatteners/flatten_type_def_to.d.ts +4 -0
- package/.out/transformers/flatteners/flatten_type_def_to.js +49 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_type_defs_of.js +7 -0
- package/.out/transformers/flatteners/flatten_value_type_to.d.ts +11 -0
- package/.out/transformers/flatteners/flatten_value_type_to.js +79 -0
- package/.out/transformers/flatteners/flatten_value_types_of.d.ts +3 -0
- package/.out/transformers/flatteners/flatten_value_types_of.js +7 -0
- package/.out/transformers/flatteners/json_path.d.ts +2 -0
- package/.out/transformers/flatteners/json_path.js +14 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_accessors_of.tests.js +90 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.js +94 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_def_to.tests.js +110 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_type_defs_of.tests.js +17 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_type_to.tests.js +297 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/flatten_value_types_of.tests.js +37 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/value_path_to_type_path.tests.js +167 -0
- package/.out/transformers/flatteners/value_path_to_type_path.d.ts +2 -0
- package/.out/transformers/flatteners/value_path_to_type_path.js +96 -0
- package/.out/tsconfig.json +15 -0
- package/.out/tsconfig.tsbuildinfo +1 -0
- package/.out/tsup.config.d.ts +3 -0
- package/.out/tsup.config.js +12 -0
- package/.out/types/builders.d.ts +62 -0
- package/.out/types/builders.js +148 -0
- package/.out/types/definitions.d.ts +41 -0
- package/.out/types/definitions.js +26 -0
- package/.out/types/flattened.d.ts +2 -0
- package/.out/types/flattened.js +1 -0
- package/.out/types/flattened_accessors_of.d.ts +9 -0
- package/.out/types/flattened_accessors_of.js +1 -0
- package/.out/types/flattened_type_defs_of.d.ts +21 -0
- package/.out/types/flattened_type_defs_of.js +1 -0
- package/.out/types/flattened_value_types_of.d.ts +6 -0
- package/.out/types/flattened_value_types_of.js +1 -0
- package/.out/types/json_path_of.d.ts +1 -0
- package/.out/types/json_path_of.js +1 -0
- package/.out/types/json_paths_of.d.ts +16 -0
- package/.out/types/json_paths_of.js +1 -0
- package/.out/types/mobx_value_type_of.d.ts +11 -0
- package/.out/types/mobx_value_type_of.js +2 -0
- package/.out/types/partial_type_def_of.d.ts +40 -0
- package/.out/types/partial_type_def_of.js +1 -0
- package/.out/types/readonly_type_def_of.d.ts +29 -0
- package/.out/types/readonly_type_def_of.js +1 -0
- package/.out/types/specs/builder.tests.d.ts +1 -0
- package/.out/types/specs/builder.tests.js +93 -0
- package/.out/types/specs/flattened_accessors_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_accessors_of.tests.js +11 -0
- package/.out/types/specs/flattened_type_defs_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_type_defs_of.tests.js +71 -0
- package/.out/types/specs/flattened_value_types_of.tests.d.ts +1 -0
- package/.out/types/specs/flattened_value_types_of.tests.js +11 -0
- package/.out/types/specs/json_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/json_paths_of.tests.js +199 -0
- package/.out/types/specs/partial_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/partial_type_def_of.tests.js +50 -0
- package/.out/types/specs/readonly_type_def_of.tests.d.ts +1 -0
- package/.out/types/specs/readonly_type_def_of.tests.js +55 -0
- package/.out/types/specs/strict_definitions.tests.d.ts +1 -0
- package/.out/types/specs/strict_definitions.tests.js +42 -0
- package/.out/types/specs/value_to_type_paths_of.tests.d.ts +1 -0
- package/.out/types/specs/value_to_type_paths_of.tests.js +90 -0
- package/.out/types/specs/value_type_of.tests.d.ts +1 -0
- package/.out/types/specs/value_type_of.tests.js +91 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.d.ts +1 -0
- package/.out/types/specs/value_types_of_discriminated_union.tests.js +9 -0
- package/.out/types/strict_definitions.d.ts +44 -0
- package/.out/types/strict_definitions.js +1 -0
- package/.out/types/value_to_type_paths_of.d.ts +22 -0
- package/.out/types/value_to_type_paths_of.js +1 -0
- package/.out/types/value_type_of.d.ts +24 -0
- package/.out/types/value_type_of.js +1 -0
- package/.out/types/value_types_of_discriminated_union.d.ts +9 -0
- package/.out/types/value_types_of_discriminated_union.js +1 -0
- package/.out/vitest.workspace.d.ts +2 -0
- package/.out/vitest.workspace.js +7 -0
- package/.turbo/turbo-build.log +18 -0
- package/.turbo/turbo-check-types.log +3 -0
- package/.turbo/turbo-release$colon$exports.log +3 -0
- package/README.md +12 -0
- package/dist/index.cjs +798 -0
- package/dist/index.d.cts +301 -0
- package/dist/index.d.ts +301 -0
- package/dist/index.js +775 -0
- package/index.ts +20 -0
- package/package.exports.json +18 -0
- package/package.json +45 -0
- package/transformers/copies/copy.ts +18 -0
- package/transformers/copies/copy_to.ts +226 -0
- package/transformers/copies/mobx_copy.ts +81 -0
- package/transformers/copies/specs/copy_to.tests.ts +143 -0
- package/transformers/copies/specs/mobx_copy.tests.ts +26 -0
- package/transformers/flatteners/flatten_accessors_of.ts +43 -0
- package/transformers/flatteners/flatten_json_value_to_type_paths_of.ts +39 -0
- package/transformers/flatteners/flatten_type_def_to.ts +127 -0
- package/transformers/flatteners/flatten_type_defs_of.ts +16 -0
- package/transformers/flatteners/flatten_value_type_to.ts +227 -0
- package/transformers/flatteners/flatten_value_types_of.ts +23 -0
- package/transformers/flatteners/json_path.ts +15 -0
- package/transformers/flatteners/specs/flatten_accessors_of.tests.ts +113 -0
- package/transformers/flatteners/specs/flatten_json_value_to_type_paths_of.tests.ts +115 -0
- package/transformers/flatteners/specs/flatten_type_def_to.tests.ts +146 -0
- package/transformers/flatteners/specs/flatten_type_defs_of.tests.ts +26 -0
- package/transformers/flatteners/specs/flatten_value_type_to.tests.ts +452 -0
- package/transformers/flatteners/specs/flatten_value_types_of.tests.ts +46 -0
- package/transformers/flatteners/specs/value_path_to_type_path.tests.ts +240 -0
- package/transformers/flatteners/value_path_to_type_path.ts +164 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +15 -0
- package/tsup.config.ts +16 -0
- package/types/builders.ts +284 -0
- package/types/definitions.ts +106 -0
- package/types/flattened.ts +5 -0
- package/types/flattened_accessors_of.ts +15 -0
- package/types/flattened_type_defs_of.ts +130 -0
- package/types/flattened_value_types_of.ts +11 -0
- package/types/json_path_of.ts +7 -0
- package/types/json_paths_of.ts +129 -0
- package/types/mobx_value_type_of.ts +16 -0
- package/types/partial_type_def_of.ts +64 -0
- package/types/readonly_type_def_of.ts +53 -0
- package/types/specs/builder.tests.ts +295 -0
- package/types/specs/flattened_accessors_of.tests.ts +27 -0
- package/types/specs/flattened_type_defs_of.tests.ts +212 -0
- package/types/specs/flattened_value_types_of.tests.ts +21 -0
- package/types/specs/json_paths_of.tests.ts +304 -0
- package/types/specs/partial_type_def_of.tests.ts +251 -0
- package/types/specs/readonly_type_def_of.tests.ts +158 -0
- package/types/specs/strict_definitions.tests.ts +184 -0
- package/types/specs/value_to_type_paths_of.tests.ts +181 -0
- package/types/specs/value_type_of.tests.ts +329 -0
- package/types/specs/value_types_of_discriminated_union.tests.ts +30 -0
- package/types/strict_definitions.ts +107 -0
- package/types/value_to_type_paths_of.ts +184 -0
- package/types/value_type_of.ts +84 -0
- package/types/value_types_of_discriminated_union.ts +14 -0
- package/vitest.workspace.ts +11 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type SimplifyDeep } from 'type-fest'
|
|
2
|
+
import {
|
|
3
|
+
booleanType,
|
|
4
|
+
numberType,
|
|
5
|
+
object,
|
|
6
|
+
union,
|
|
7
|
+
} from 'types/builders'
|
|
8
|
+
import { type ValueTypesOfDiscriminatedUnion } from 'types/value_types_of_discriminated_union'
|
|
9
|
+
|
|
10
|
+
describe('ValueTypesOfDiscriminatedUnion', function () {
|
|
11
|
+
it('matches expected type', function () {
|
|
12
|
+
const { definition } = union('d')
|
|
13
|
+
.add('a', object().set('x', numberType))
|
|
14
|
+
.add('b', object().set('y', booleanType))
|
|
15
|
+
type T = SimplifyDeep<ValueTypesOfDiscriminatedUnion<
|
|
16
|
+
typeof definition
|
|
17
|
+
>>
|
|
18
|
+
|
|
19
|
+
expectTypeOf<T>().toEqualTypeOf<{
|
|
20
|
+
readonly a: {
|
|
21
|
+
d: 'a',
|
|
22
|
+
x: number,
|
|
23
|
+
},
|
|
24
|
+
readonly b: {
|
|
25
|
+
d: 'b',
|
|
26
|
+
y: boolean,
|
|
27
|
+
},
|
|
28
|
+
}>()
|
|
29
|
+
})
|
|
30
|
+
})
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type IsEqual,
|
|
3
|
+
type Simplify,
|
|
4
|
+
} from 'type-fest'
|
|
5
|
+
import {
|
|
6
|
+
type ObjectFieldKey,
|
|
7
|
+
type RecordKeyType,
|
|
8
|
+
type TypeDefType,
|
|
9
|
+
type UnionKey,
|
|
10
|
+
} from './definitions'
|
|
11
|
+
|
|
12
|
+
// TODO rename to something more descriptive (e.g. introspectable definitions, deterministic
|
|
13
|
+
// definitions, discriminated definitions). Maybe should just replace definitions since most
|
|
14
|
+
// of the supporting types and utilities don't support non-strict definitions anyway
|
|
15
|
+
|
|
16
|
+
// strict equivalent of type defs, basically just makes it so Union is introspectable
|
|
17
|
+
|
|
18
|
+
export type StrictType<T extends StrictTypeDef = StrictTypeDef> = {
|
|
19
|
+
readonly definition: T,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type StrictTypeDef =
|
|
23
|
+
| StrictLiteralTypeDef
|
|
24
|
+
| StrictListTypeDef
|
|
25
|
+
| StrictRecordTypeDef
|
|
26
|
+
| StrictObjectTypeDef
|
|
27
|
+
| StrictUnionTypeDef
|
|
28
|
+
|
|
29
|
+
// used to avoid TS complaining about circular references
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
type AnyTypeDef = any
|
|
32
|
+
|
|
33
|
+
// literal
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
export type StrictLiteralTypeDef<V = any> = {
|
|
36
|
+
readonly type: TypeDefType.Literal,
|
|
37
|
+
readonly valuePrototype: [V],
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// list
|
|
41
|
+
export type StrictListTypeDef<
|
|
42
|
+
E extends StrictTypeDef = AnyTypeDef,
|
|
43
|
+
> = {
|
|
44
|
+
readonly type: TypeDefType.List,
|
|
45
|
+
// readonly is inherited by the output
|
|
46
|
+
readonly elements: E,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// map
|
|
50
|
+
export type StrictRecordTypeDef<
|
|
51
|
+
K extends RecordKeyType = RecordKeyType,
|
|
52
|
+
// if `V` includes `undefined` the map is partial
|
|
53
|
+
V extends StrictTypeDef | undefined = AnyTypeDef,
|
|
54
|
+
> = {
|
|
55
|
+
readonly type: TypeDefType.Record,
|
|
56
|
+
// never actually populate
|
|
57
|
+
readonly keyPrototype: K,
|
|
58
|
+
// readonly is inherited by the output
|
|
59
|
+
readonly valueTypeDef: V,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// structured type
|
|
63
|
+
|
|
64
|
+
// NOTE we use the `readonly` and `?` (partial) status of these field definitions
|
|
65
|
+
// to describe the same attributes of the fields
|
|
66
|
+
export type StrictStructuredTypeDefFields = {
|
|
67
|
+
[Key: ObjectFieldKey]: AnyTypeDef,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// NOTE: we cannot collapse this type to
|
|
71
|
+
// `StructuredTypeDef = StructuredTypeDefFields`
|
|
72
|
+
// as we rely on the `fields` field being unique to discriminate between different
|
|
73
|
+
// TypeDefs
|
|
74
|
+
export type StrictObjectTypeDef<
|
|
75
|
+
Fields extends StrictStructuredTypeDefFields = StrictStructuredTypeDefFields,
|
|
76
|
+
> = {
|
|
77
|
+
readonly type: TypeDefType.Object,
|
|
78
|
+
readonly fields: Fields,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type StrictUnionTypeDef<
|
|
82
|
+
D extends string | null = string | null,
|
|
83
|
+
U extends Readonly<Record<UnionKey, AnyTypeDef>> = Readonly<Record<UnionKey, AnyTypeDef>>,
|
|
84
|
+
> = D extends null ? IsStrictUnion<U> extends true ? {
|
|
85
|
+
readonly discriminator: null,
|
|
86
|
+
readonly type: TypeDefType.Union,
|
|
87
|
+
readonly unions: U,
|
|
88
|
+
}
|
|
89
|
+
: never
|
|
90
|
+
// TODO enforce the unions are all structs
|
|
91
|
+
: {
|
|
92
|
+
readonly discriminator: D,
|
|
93
|
+
readonly type: TypeDefType.Union,
|
|
94
|
+
readonly unions: U,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// tests whether the union is composed of one non-constant value (at ['0']) and the rest
|
|
98
|
+
// constants
|
|
99
|
+
export type IsStrictUnion<U extends Readonly<Record<UnionKey, AnyTypeDef>>> = IsEqual<
|
|
100
|
+
U,
|
|
101
|
+
Simplify<{
|
|
102
|
+
readonly [K in keyof Omit<U, '0'> as U[K] extends StrictLiteralTypeDef ? K : never]: U[K]
|
|
103
|
+
} & (U extends { readonly ['0']: AnyTypeDef } ? {
|
|
104
|
+
readonly ['0']: U['0'],
|
|
105
|
+
}
|
|
106
|
+
: {})>
|
|
107
|
+
>
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type SimplifyDeep,
|
|
3
|
+
type UnionToIntersection,
|
|
4
|
+
} from 'type-fest'
|
|
5
|
+
import {
|
|
6
|
+
type ListTypeDef,
|
|
7
|
+
type LiteralTypeDef,
|
|
8
|
+
type ObjectTypeDef,
|
|
9
|
+
type RecordTypeDef,
|
|
10
|
+
type TypeDef,
|
|
11
|
+
type UnionTypeDef,
|
|
12
|
+
} from './definitions'
|
|
13
|
+
import {
|
|
14
|
+
type Depths,
|
|
15
|
+
type StartingDepth,
|
|
16
|
+
} from './flattened'
|
|
17
|
+
import { type JsonPathOf } from './json_path_of'
|
|
18
|
+
import { type StrictType } from './strict_definitions'
|
|
19
|
+
|
|
20
|
+
export type ValueToTypePathsOf<
|
|
21
|
+
T extends StrictType,
|
|
22
|
+
SegmentOverride extends string = '*',
|
|
23
|
+
Path extends string = '$',
|
|
24
|
+
> = SimplifyDeep<InternalFlattenedJsonPathsOf<
|
|
25
|
+
T['definition'],
|
|
26
|
+
SegmentOverride,
|
|
27
|
+
Path,
|
|
28
|
+
Path,
|
|
29
|
+
StartingDepth
|
|
30
|
+
>>
|
|
31
|
+
|
|
32
|
+
type InternalFlattenedJsonPathsOf<
|
|
33
|
+
T extends TypeDef,
|
|
34
|
+
SegmentOverride extends string,
|
|
35
|
+
ValuePath extends string,
|
|
36
|
+
TypePath extends string,
|
|
37
|
+
Depth extends number,
|
|
38
|
+
> =
|
|
39
|
+
& {
|
|
40
|
+
readonly [K in ValuePath]: TypePath
|
|
41
|
+
}
|
|
42
|
+
& InternalFlattenedJsonPathsOfChildren<T, SegmentOverride, ValuePath, TypePath, '', Depth>
|
|
43
|
+
|
|
44
|
+
type InternalFlattenedJsonPathsOfChildren<
|
|
45
|
+
T extends TypeDef,
|
|
46
|
+
SegmentOverride extends string,
|
|
47
|
+
ValuePath extends string,
|
|
48
|
+
TypePath extends string,
|
|
49
|
+
Qualifier extends string,
|
|
50
|
+
Depth extends number,
|
|
51
|
+
NextDepth extends number = Depths[Depth],
|
|
52
|
+
> = NextDepth extends -1 ? never
|
|
53
|
+
: T extends LiteralTypeDef ? InternalFlattenedJsonPathsOfLiteralChildren
|
|
54
|
+
: T extends ListTypeDef ? InternalFlattenedJsonPathsOfListChildren<
|
|
55
|
+
T,
|
|
56
|
+
SegmentOverride,
|
|
57
|
+
ValuePath,
|
|
58
|
+
TypePath,
|
|
59
|
+
NextDepth
|
|
60
|
+
>
|
|
61
|
+
: T extends RecordTypeDef ? InternalFlattenedJsonPathsOfRecordChildren<
|
|
62
|
+
T,
|
|
63
|
+
SegmentOverride,
|
|
64
|
+
ValuePath,
|
|
65
|
+
TypePath,
|
|
66
|
+
NextDepth
|
|
67
|
+
>
|
|
68
|
+
: T extends ObjectTypeDef ? InternalFlattenedJsonPathsOfObjectChildren<
|
|
69
|
+
T,
|
|
70
|
+
SegmentOverride,
|
|
71
|
+
ValuePath,
|
|
72
|
+
TypePath,
|
|
73
|
+
Qualifier,
|
|
74
|
+
NextDepth
|
|
75
|
+
>
|
|
76
|
+
: T extends UnionTypeDef ? InternalFlattenedJsonPathsOfUnionChildren<
|
|
77
|
+
T,
|
|
78
|
+
SegmentOverride,
|
|
79
|
+
ValuePath,
|
|
80
|
+
TypePath,
|
|
81
|
+
Qualifier,
|
|
82
|
+
NextDepth
|
|
83
|
+
>
|
|
84
|
+
: never
|
|
85
|
+
|
|
86
|
+
type InternalFlattenedJsonPathsOfLiteralChildren = {}
|
|
87
|
+
|
|
88
|
+
type InternalFlattenedJsonPathsOfListChildren<
|
|
89
|
+
T extends ListTypeDef,
|
|
90
|
+
SegmentOverride extends string,
|
|
91
|
+
ValuePath extends string,
|
|
92
|
+
TypePath extends string,
|
|
93
|
+
Depth extends number,
|
|
94
|
+
> = InternalFlattenedJsonPathsOf<
|
|
95
|
+
T['elements'],
|
|
96
|
+
SegmentOverride,
|
|
97
|
+
JsonPathOf<ValuePath, number>,
|
|
98
|
+
JsonPathOf<TypePath, number, SegmentOverride>,
|
|
99
|
+
Depth
|
|
100
|
+
>
|
|
101
|
+
|
|
102
|
+
type InternalFlattenedJsonPathsOfRecordChildren<
|
|
103
|
+
T extends RecordTypeDef,
|
|
104
|
+
SegmentOverride extends string,
|
|
105
|
+
ValuePath extends string,
|
|
106
|
+
TypePath extends string,
|
|
107
|
+
Depth extends number,
|
|
108
|
+
> = InternalFlattenedJsonPathsOf<
|
|
109
|
+
T['valueTypeDef'],
|
|
110
|
+
SegmentOverride,
|
|
111
|
+
JsonPathOf<ValuePath, T['keyPrototype']>,
|
|
112
|
+
JsonPathOf<TypePath, T['keyPrototype'], SegmentOverride>,
|
|
113
|
+
Depth
|
|
114
|
+
>
|
|
115
|
+
|
|
116
|
+
type InternalFlattenedJsonPathsOfObjectChildren<
|
|
117
|
+
T extends ObjectTypeDef,
|
|
118
|
+
SegmentOverride extends string,
|
|
119
|
+
ValuePath extends string,
|
|
120
|
+
TypePath extends string,
|
|
121
|
+
Qualifier extends string,
|
|
122
|
+
Depth extends number,
|
|
123
|
+
> = T extends ObjectTypeDef<infer Fields> ? keyof Fields extends string ? UnionToIntersection<
|
|
124
|
+
{
|
|
125
|
+
readonly [K in keyof Fields]-?: InternalFlattenedJsonPathsOf<
|
|
126
|
+
Exclude<Fields[K], undefined>,
|
|
127
|
+
SegmentOverride,
|
|
128
|
+
JsonPathOf<ValuePath, `${Qualifier}${K}`>,
|
|
129
|
+
JsonPathOf<TypePath, `${Qualifier}${K}`>,
|
|
130
|
+
Depth
|
|
131
|
+
>
|
|
132
|
+
}[keyof Fields]
|
|
133
|
+
>
|
|
134
|
+
: never
|
|
135
|
+
: never
|
|
136
|
+
|
|
137
|
+
// type InternalFlattenedJsonPathsOfUnionChildren<
|
|
138
|
+
// T extends UnionTypeDef,
|
|
139
|
+
// SegmentOverride extends string,
|
|
140
|
+
// ValuePath extends string,
|
|
141
|
+
// TypePath extends string,
|
|
142
|
+
// Depth extends number,
|
|
143
|
+
// > = T extends UnionTypeDef<infer D, infer Unions> ?
|
|
144
|
+
// & ({
|
|
145
|
+
// [K in keyof Unions]: InternalFlattenedJsonPathsOfChildren<
|
|
146
|
+
// Unions[K],
|
|
147
|
+
// SegmentOverride,
|
|
148
|
+
// ValuePath,
|
|
149
|
+
// TypePath,
|
|
150
|
+
// Depth
|
|
151
|
+
// >
|
|
152
|
+
// }[keyof Unions])
|
|
153
|
+
// & (D extends string ? ReadonlyRecord<JsonPathOf<ValuePath, D>, JsonPathOf<TypePath, D>> : {})
|
|
154
|
+
// : never
|
|
155
|
+
|
|
156
|
+
type InternalFlattenedJsonPathsOfUnionChildren<
|
|
157
|
+
T extends UnionTypeDef,
|
|
158
|
+
SegmentOverride extends string,
|
|
159
|
+
ValuePath extends string,
|
|
160
|
+
TypePath extends string,
|
|
161
|
+
Qualifier extends string,
|
|
162
|
+
Depth extends number,
|
|
163
|
+
> = T extends UnionTypeDef<infer D, infer Unions> ? keyof Unions extends string ? D extends null ? UnionToIntersection<{
|
|
164
|
+
readonly [K in keyof Unions]: InternalFlattenedJsonPathsOfChildren<
|
|
165
|
+
Unions[K],
|
|
166
|
+
SegmentOverride,
|
|
167
|
+
ValuePath,
|
|
168
|
+
TypePath,
|
|
169
|
+
'',
|
|
170
|
+
Depth
|
|
171
|
+
>
|
|
172
|
+
}[keyof Unions]>
|
|
173
|
+
: UnionToIntersection<{
|
|
174
|
+
readonly [K in keyof Unions]: InternalFlattenedJsonPathsOfChildren<
|
|
175
|
+
Unions[K],
|
|
176
|
+
SegmentOverride,
|
|
177
|
+
ValuePath,
|
|
178
|
+
TypePath,
|
|
179
|
+
`${Qualifier}${K}:`,
|
|
180
|
+
Depth
|
|
181
|
+
>
|
|
182
|
+
}[keyof Unions]>
|
|
183
|
+
: never
|
|
184
|
+
: never
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type IsFieldReadonly,
|
|
3
|
+
} from '@strictly/base'
|
|
4
|
+
import {
|
|
5
|
+
type ListTypeDef,
|
|
6
|
+
type LiteralTypeDef,
|
|
7
|
+
type ObjectTypeDef,
|
|
8
|
+
type RecordTypeDef,
|
|
9
|
+
type Type,
|
|
10
|
+
type TypeDef,
|
|
11
|
+
type UnionTypeDef,
|
|
12
|
+
} from './definitions'
|
|
13
|
+
|
|
14
|
+
export type ValueTypeOf<
|
|
15
|
+
T,
|
|
16
|
+
Extra = {},
|
|
17
|
+
> = T extends Type ? InternalValueTypeOf<T['definition'], Extra> : never
|
|
18
|
+
|
|
19
|
+
export type InternalValueTypeOf<
|
|
20
|
+
F extends TypeDef,
|
|
21
|
+
Extra,
|
|
22
|
+
> = F extends LiteralTypeDef ? InternalValueTypeOfLiteral<F>
|
|
23
|
+
: F extends ListTypeDef ? InternalValueTypeOfList<F, Extra>
|
|
24
|
+
: F extends RecordTypeDef ? InternalValueTypeOfRecord<F, Extra>
|
|
25
|
+
: F extends ObjectTypeDef ? InternalValueTypeOfObject<F, Extra>
|
|
26
|
+
: F extends UnionTypeDef ? InternalValueTypeOfUnion<F, Extra>
|
|
27
|
+
: never
|
|
28
|
+
|
|
29
|
+
type InternalValueTypeOfLiteral<F extends LiteralTypeDef> = F['valuePrototype'][number]
|
|
30
|
+
|
|
31
|
+
type InternalValueTypeOfList<F extends ListTypeDef, Extra> = IsFieldReadonly<F, 'elements'> extends true
|
|
32
|
+
? readonly InternalValueTypeOf<
|
|
33
|
+
F['elements'],
|
|
34
|
+
Extra
|
|
35
|
+
>[] & Extra
|
|
36
|
+
: InternalValueTypeOf<
|
|
37
|
+
F['elements'],
|
|
38
|
+
Extra
|
|
39
|
+
>[] & Extra
|
|
40
|
+
|
|
41
|
+
type InternalValueTypeOfRecord<
|
|
42
|
+
F extends RecordTypeDef,
|
|
43
|
+
Extra,
|
|
44
|
+
> = undefined extends F['valueTypeDef']
|
|
45
|
+
// partial
|
|
46
|
+
? IsFieldReadonly<F, 'valueTypeDef'> extends true
|
|
47
|
+
// readonly
|
|
48
|
+
? {
|
|
49
|
+
readonly [k in F['keyPrototype']]?: InternalValueTypeOf<F['valueTypeDef'], Extra>
|
|
50
|
+
}
|
|
51
|
+
: {
|
|
52
|
+
[k in F['keyPrototype']]?: InternalValueTypeOf<F['valueTypeDef'], Extra>
|
|
53
|
+
}
|
|
54
|
+
// complete
|
|
55
|
+
: IsFieldReadonly<F, 'valueTypeDef'> extends true
|
|
56
|
+
// readonly
|
|
57
|
+
? {
|
|
58
|
+
readonly [k in F['keyPrototype']]: InternalValueTypeOf<F['valueTypeDef'], Extra>
|
|
59
|
+
}
|
|
60
|
+
: {
|
|
61
|
+
[k in F['keyPrototype']]: InternalValueTypeOf<F['valueTypeDef'], Extra>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type InternalValueTypeOfObject<
|
|
65
|
+
F extends ObjectTypeDef,
|
|
66
|
+
Extra,
|
|
67
|
+
> = F extends ObjectTypeDef<infer Fields> ? {
|
|
68
|
+
[K in keyof Fields]: InternalValueTypeOf<
|
|
69
|
+
Fields[K],
|
|
70
|
+
Extra
|
|
71
|
+
>
|
|
72
|
+
} & Extra
|
|
73
|
+
: never
|
|
74
|
+
|
|
75
|
+
type InternalValueTypeOfUnion<
|
|
76
|
+
F extends UnionTypeDef,
|
|
77
|
+
Extra,
|
|
78
|
+
> = F extends UnionTypeDef<infer D, infer U> ? D extends string ? {
|
|
79
|
+
[K in keyof U]: InternalValueTypeOf<U[K], Extra> & Readonly<Record<D, K>>
|
|
80
|
+
}[keyof U]
|
|
81
|
+
: {
|
|
82
|
+
[K in keyof U]: InternalValueTypeOf<U[K], Extra>
|
|
83
|
+
}[keyof U]
|
|
84
|
+
: never
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type UnionTypeDef } from './definitions'
|
|
2
|
+
import { type ValueTypeOf } from './value_type_of'
|
|
3
|
+
|
|
4
|
+
// gets a record of the values types for a discriminated union mapped according to
|
|
5
|
+
// their discriminators
|
|
6
|
+
export type ValueTypesOfDiscriminatedUnion<
|
|
7
|
+
U extends UnionTypeDef,
|
|
8
|
+
> = U extends UnionTypeDef<infer D, infer Unions> ? D extends string ? {
|
|
9
|
+
[K in keyof Unions]: ValueTypeOf<{ definition: Unions[K] }> & {
|
|
10
|
+
[KD in D]: K
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
: never
|
|
14
|
+
: never
|