arktype 2.0.0-dev.6 → 2.0.0-dev.8

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.
Files changed (98) hide show
  1. package/__tests__/array.test.ts +23 -25
  2. package/__tests__/config.test.ts +8 -8
  3. package/__tests__/cyclic.test.ts +3 -3
  4. package/__tests__/examples.test.ts +3 -3
  5. package/__tests__/instanceof.test.ts +7 -7
  6. package/__tests__/intersection.test.ts +2 -2
  7. package/__tests__/keyTraversal.test.ts +15 -15
  8. package/__tests__/keywords.test.ts +50 -37
  9. package/__tests__/literal.test.ts +2 -2
  10. package/__tests__/match.bench.ts +6 -6
  11. package/__tests__/narrow.test.ts +17 -23
  12. package/__tests__/objectLiteral.test.ts +4 -4
  13. package/__tests__/operator.bench.ts +4 -4
  14. package/__tests__/{morph.test.ts → pipe.test.ts} +136 -75
  15. package/__tests__/scope.test.ts +5 -15
  16. package/__tests__/traverse.test.ts +36 -35
  17. package/__tests__/type.test.ts +8 -10
  18. package/ark.ts +14 -11
  19. package/generic.ts +8 -6
  20. package/main.ts +3 -3
  21. package/match.ts +59 -70
  22. package/out/ark.d.ts +4 -3
  23. package/out/ark.d.ts.map +1 -1
  24. package/out/ark.js +4 -4
  25. package/out/ark.js.map +1 -1
  26. package/out/generic.d.ts.map +1 -1
  27. package/out/generic.js +8 -6
  28. package/out/generic.js.map +1 -1
  29. package/out/main.d.ts +3 -3
  30. package/out/main.d.ts.map +1 -1
  31. package/out/main.js +3 -2
  32. package/out/main.js.map +1 -1
  33. package/out/match.d.ts.map +1 -1
  34. package/out/match.js +50 -56
  35. package/out/match.js.map +1 -1
  36. package/out/parser/generic.d.ts +3 -3
  37. package/out/parser/generic.d.ts.map +1 -1
  38. package/out/parser/generic.js +4 -5
  39. package/out/parser/generic.js.map +1 -1
  40. package/out/parser/objectLiteral.d.ts.map +1 -1
  41. package/out/parser/objectLiteral.js +2 -3
  42. package/out/parser/objectLiteral.js.map +1 -1
  43. package/out/parser/semantic/validate.d.ts +2 -2
  44. package/out/parser/semantic/validate.d.ts.map +1 -1
  45. package/out/parser/semantic/validate.js.map +1 -1
  46. package/out/parser/string/reduce/dynamic.d.ts.map +1 -1
  47. package/out/parser/string/reduce/dynamic.js +4 -8
  48. package/out/parser/string/reduce/dynamic.js.map +1 -1
  49. package/out/parser/string/shift/operand/date.js +2 -4
  50. package/out/parser/string/shift/operand/date.js.map +1 -1
  51. package/out/parser/string/shift/operand/enclosed.js +6 -8
  52. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  53. package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
  54. package/out/parser/string/shift/operand/genericArgs.js +4 -5
  55. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  56. package/out/parser/string/shift/operand/unenclosed.d.ts +4 -4
  57. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -1
  58. package/out/parser/string/shift/operand/unenclosed.js +5 -10
  59. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  60. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -1
  61. package/out/parser/string/shift/operator/bounds.js +4 -8
  62. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  63. package/out/parser/string/shift/operator/divisor.d.ts.map +1 -1
  64. package/out/parser/string/shift/operator/divisor.js +1 -2
  65. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  66. package/out/parser/string/shift/scanner.d.ts.map +1 -1
  67. package/out/parser/string/shift/scanner.js +2 -4
  68. package/out/parser/string/shift/scanner.js.map +1 -1
  69. package/out/parser/string/string.d.ts.map +1 -1
  70. package/out/parser/string/string.js +1 -2
  71. package/out/parser/string/string.js.map +1 -1
  72. package/out/parser/tuple.d.ts.map +1 -1
  73. package/out/parser/tuple.js +12 -15
  74. package/out/parser/tuple.js.map +1 -1
  75. package/out/scope.d.ts +9 -9
  76. package/out/scope.d.ts.map +1 -1
  77. package/out/scope.js +11 -33
  78. package/out/scope.js.map +1 -1
  79. package/out/type.d.ts +38 -18
  80. package/out/type.d.ts.map +1 -1
  81. package/out/type.js +32 -1
  82. package/out/type.js.map +1 -1
  83. package/package.json +3 -3
  84. package/parser/generic.ts +11 -12
  85. package/parser/objectLiteral.ts +2 -4
  86. package/parser/semantic/validate.ts +2 -3
  87. package/parser/string/reduce/dynamic.ts +7 -10
  88. package/parser/string/shift/operand/date.ts +3 -6
  89. package/parser/string/shift/operand/enclosed.ts +8 -8
  90. package/parser/string/shift/operand/genericArgs.ts +8 -8
  91. package/parser/string/shift/operand/unenclosed.ts +14 -23
  92. package/parser/string/shift/operator/bounds.ts +7 -9
  93. package/parser/string/shift/operator/divisor.ts +2 -3
  94. package/parser/string/shift/scanner.ts +2 -4
  95. package/parser/string/string.ts +2 -3
  96. package/parser/tuple.ts +15 -16
  97. package/scope.ts +24 -48
  98. package/type.ts +150 -35
@@ -1,7 +1,7 @@
1
1
  import { attest, contextualize } from "@arktype/attest"
2
2
  import type { Narrowed, Out, of, string } from "@arktype/schema"
3
- import { type equals, reference } from "@arktype/util"
4
- import { type Type, type } from "arktype"
3
+ import { reference, type equals } from "@arktype/util"
4
+ import { type, type Type } from "arktype"
5
5
 
6
6
  contextualize(() => {
7
7
  it("implicit problem", () => {
@@ -10,15 +10,13 @@ contextualize(() => {
10
10
  const odd = type(["number", ":", isOdd])
11
11
  attest<number>(odd.infer)
12
12
  attest(odd.json).equals({ domain: "number", predicate: [isOddRef] })
13
- attest(odd(1).out).equals(1)
14
- attest(odd(2).errors?.summary).snap(
15
- "must be valid according to isOdd (was 2)"
16
- )
13
+ attest(odd(1)).equals(1)
14
+ attest(odd(2).toString()).snap("must be valid according to isOdd (was 2)")
17
15
  })
18
16
 
19
17
  it("implicit problem anonymous", () => {
20
- const even = type("number", ":", (n) => n % 2 === 0)
21
- attest(even(1).errors?.summary).snap(
18
+ const even = type("number", ":", n => n % 2 === 0)
19
+ attest(even(1).toString()).snap(
22
20
  "must be valid according to an anonymous predicate (was 1)"
23
21
  )
24
22
  })
@@ -27,9 +25,9 @@ contextualize(() => {
27
25
  const even = type([
28
26
  "number",
29
27
  ":",
30
- (n, ctx) => n % 3 === 0 || ctx.invalid({ expected: "divisible by 3" })
28
+ (n, ctx) => n % 3 === 0 || ctx.invalid("divisible by 3")
31
29
  ])
32
- attest(even(1).errors?.summary).snap("must be divisible by 3 (was 1)")
30
+ attest(even(1).toString()).snap("must be divisible by 3 (was 1)")
33
31
  })
34
32
 
35
33
  it("problem at path", () => {
@@ -40,16 +38,15 @@ contextualize(() => {
40
38
  },
41
39
  ":",
42
40
  ({ a, b }, ctx) => {
43
- if (a === b) {
44
- return true
45
- }
41
+ if (a === b) return true
42
+
46
43
  ctx.error({ expected: "equal to b", path: ["a"] })
47
44
  ctx.error({ expected: "equal to a", path: ["b"] })
48
45
  return false
49
46
  }
50
47
  ])
51
- attest(abEqual({ a: 1, b: 1 }).out).equals({ a: 1, b: 1 })
52
- attest(abEqual({ a: 1, b: 2 }).errors?.summary).snap(
48
+ attest(abEqual({ a: 1, b: 1 })).equals({ a: 1, b: 1 })
49
+ attest(abEqual({ a: 1, b: 2 }).toString()).snap(
53
50
  'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
54
51
  )
55
52
  })
@@ -65,11 +62,8 @@ contextualize(() => {
65
62
  t: equals<t, Expected> extends true ? t : Expected
66
63
  ) => true
67
64
  attest<number | boolean[]>(
68
- type([
69
- "number|boolean[]",
70
- ":",
71
- (data) => validateNumberOrBooleanList(data)
72
- ]).infer
65
+ type(["number|boolean[]", ":", data => validateNumberOrBooleanList(data)])
66
+ .infer
73
67
  )
74
68
  attest(() => {
75
69
  // @ts-expect-error
@@ -85,15 +79,15 @@ contextualize(() => {
85
79
  s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
86
80
  ])
87
81
  attest<Type<string.narrowed>>(palindrome)
88
- attest(palindrome("dad").out).snap("dad")
89
- attest(palindrome("david").errors?.summary).snap(
82
+ attest(palindrome("dad")).snap("dad")
83
+ attest(palindrome("david").toString()).snap(
90
84
  'must be a palindrome (was "david")'
91
85
  )
92
86
  })
93
87
 
94
88
  it("narrows the output type of a morph", () => {
95
89
  const t = type("string")
96
- .morph((s) => s.length)
90
+ .pipe(s => s.length)
97
91
  .narrow((n): n is 5 => n === 5)
98
92
 
99
93
  attest<Type<(In: string) => Out<of<5, Narrowed>>, {}>>(t)
@@ -92,16 +92,16 @@ contextualize(
92
92
  // TODO: reenable
93
93
  // it("traverse optional", () => {
94
94
  // const o = type({ "a?": "string" }).configure({ keys: "strict" })
95
- // attest(o({ a: "a" }).out).snap({ a: "a" })
96
- // attest(o({}).out).snap({})
97
- // attest(o({ a: 1 }).errors?.summary).snap("a must be a string (was number)")
95
+ // attest(o({ a: "a" })).snap({ a: "a" })
96
+ // attest(o({})).snap({})
97
+ // attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
98
98
  // })
99
99
 
100
100
  // it("multiple bad strict", () => {
101
101
  // const t = type({ a: "string", b: "boolean" }).configure({
102
102
  // keys: "strict"
103
103
  // })
104
- // attest(t({ a: 1, b: 2 }).errors?.summary).snap(
104
+ // attest(t({ a: 1, b: 2 }).toString()).snap(
105
105
  // "a must be a string (was number)\nb must be boolean (was number)"
106
106
  // )
107
107
  // })
@@ -106,25 +106,25 @@ bench("divisor", () => {
106
106
  .types([1023, "instantiations"])
107
107
 
108
108
  bench("filter-tuple", () => {
109
- const _ = type(["boolean", ":", (b) => b])
109
+ const _ = type(["boolean", ":", b => b])
110
110
  })
111
111
  .median([8.04, "us"])
112
112
  .types([583, "instantiations"])
113
113
 
114
114
  bench("filter-chain", () => {
115
- const _ = type("boolean").narrow((b) => b)
115
+ const _ = type("boolean").narrow(b => b)
116
116
  })
117
117
  .median([12.19, "us"])
118
118
  .types([42, "instantiations"])
119
119
 
120
120
  bench("morph-tuple", () => {
121
- const _ = type(["boolean", "=>", (b) => b])
121
+ const _ = type(["boolean", "=>", b => b])
122
122
  })
123
123
  .median([8.96, "us"])
124
124
  .types([562, "instantiations"])
125
125
 
126
126
  bench("morph-chain", () => {
127
- const _ = type("boolean").morph((b) => b)
127
+ const _ = type("boolean").pipe(b => b)
128
128
  })
129
129
  .median([2.98, "us"])
130
130
  .types([26, "instantiations"])
@@ -1,110 +1,172 @@
1
1
  import { attest, contextualize } from "@arktype/attest"
2
- import { type Out, assertNodeKind } from "@arktype/schema"
3
- import { type Type, scope, type } from "arktype"
2
+ import { assertNodeKind, type Out } from "@arktype/schema"
3
+ import { scope, type, type Type } from "arktype"
4
4
 
5
5
  contextualize(() => {
6
6
  it("base", () => {
7
- const t = type("number").morph((data) => `${data}`)
7
+ const t = type("number").pipe(data => `${data}`)
8
8
  attest<Type<(In: number) => Out<string>>>(t)
9
9
  attest<string>(t.infer)
10
10
  attest<number>(t.in.infer)
11
- const { out } = t(5)
12
- attest<string | undefined>(out).equals("5")
13
- const { errors } = t("foo")
14
- attest(errors?.summary).snap("must be a number (was string)")
11
+ const out = t(5)
12
+ attest<string | type.errors>(out).equals("5")
13
+ const result = t("foo")
14
+ attest(result.toString()).snap("must be a number (was string)")
15
15
  })
16
16
 
17
17
  it("within type", () => {
18
- const t = type(["boolean", "=>", (data) => !data])
18
+ const t = type(["boolean", "=>", data => !data])
19
19
  attest<Type<(In: boolean) => Out<boolean>>>(t)
20
- const { out } = t(true)
21
- attest<boolean | undefined>(out).equals(false)
20
+ const out = t(true)
21
+ attest<boolean | type.errors>(out).equals(false)
22
22
  })
23
23
 
24
- it("validated output", () => {
25
- const parsedUser = type("string").morph((s) => JSON.parse(s), {
24
+ it("type instance reference", () => {
25
+ const user = type({
26
26
  name: "string",
27
27
  age: "number"
28
28
  })
29
+ const parsedUser = type("string").pipe(s => JSON.parse(s), user)
30
+
29
31
  attest<
30
32
  Type<
31
33
  (In: string) => Out<{
32
34
  name: string
33
35
  age: number
34
- }>,
35
- {}
36
+ }>
36
37
  >
37
38
  >(parsedUser)
39
+
40
+ const validUser = { name: "David", age: 30 }
41
+ attest(parsedUser(JSON.stringify(validUser))).equals(validUser)
42
+ const missingKey = { name: "David" }
43
+ attest(parsedUser(JSON.stringify(missingKey)).toString()).snap(
44
+ "age must be defined"
45
+ )
46
+ })
47
+
48
+ it("many pipes", () => {
49
+ const pipeAlphabet = type("'a'").pipe(
50
+ s => `${s}b` as const,
51
+ s => `${s}c` as const,
52
+ s => `${s}d` as const,
53
+ s => `${s}e` as const,
54
+ s => `${s}f` as const,
55
+ s => `${s}g` as const,
56
+ s => `${s}h` as const
57
+ )
58
+ attest<"abcdefgh">(pipeAlphabet.infer)
59
+ attest(pipeAlphabet("a")).equals("abcdefgh")
60
+ })
61
+
62
+ it("uses pipe for consecutive types", () => {
63
+ const bar = type({ bar: "number" })
64
+ const t = type({ foo: "string" }).pipe(bar)
65
+ attest<
66
+ Type<{
67
+ foo: string
68
+ bar: number
69
+ }>
70
+ >(t)
71
+ const expected = type({ foo: "string", bar: "number" })
72
+ attest(t.json).equals(expected.json)
73
+ })
74
+
75
+ it("uses pipe for many consecutive types", () => {
76
+ const t = type({ a: "1" }).pipe(
77
+ type({ b: "1" }),
78
+ type({ c: "1" }),
79
+ type({ d: "1" })
80
+ )
81
+ attest<
82
+ Type<{
83
+ a: 1
84
+ b: 1
85
+ c: 1
86
+ d: 1
87
+ }>
88
+ >(t)
89
+ const expected = type({ a: "1", b: "1", c: "1", d: "1" })
90
+ attest(t.json).equals(expected.json)
91
+ })
92
+
93
+ it("two morphs", () => {
94
+ const inefficientStringIsEmpty = type("string").pipe(
95
+ s => s.length,
96
+ length => length === 0
97
+ )
98
+
99
+ attest<Type<(In: string) => Out<boolean>>>(inefficientStringIsEmpty)
100
+ attest(inefficientStringIsEmpty("")).equals(true)
101
+ attest(inefficientStringIsEmpty("foo")).equals(false)
102
+ attest(inefficientStringIsEmpty(0).toString()).snap(
103
+ "must be a string (was number)"
104
+ )
38
105
  })
39
106
 
40
107
  it("any as out", () => {
41
- const t = type("string", "=>", (s) => s as any)
108
+ const t = type("string", "=>", s => s as any)
42
109
  attest<string>(t.in.infer)
43
110
  attest<any>(t.infer)
44
111
  })
45
112
 
46
113
  it("never as out", () => {
47
- const t = type("string", "=>", (s) => s as never)
114
+ const t = type("string", "=>", s => s as never)
48
115
  attest<string>(t.in.infer)
49
116
  attest<never>(t.infer)
50
117
  })
51
118
 
52
- it("return problem", () => {
119
+ it("return error", () => {
53
120
  const divide100By = type("number", "=>", (n, ctx) =>
54
121
  n !== 0 ? 100 / n : ctx.error("non-zero")
55
122
  )
56
123
  attest<Type<(In: number) => Out<number>>>(divide100By)
57
- attest(divide100By(5).out).equals(20)
58
- attest(divide100By(0).errors?.summary).snap("must be non-zero (was 0)")
124
+ attest(divide100By(5)).equals(20)
125
+ attest(divide100By(0).toString()).snap("must be non-zero (was 0)")
59
126
  })
60
127
 
61
128
  it("at path", () => {
62
- const t = type({ a: ["string", "=>", (data) => data.length] })
129
+ const t = type({ a: ["string", "=>", data => data.length] })
63
130
  attest<Type<{ a: (In: string) => Out<number> }>>(t)
64
131
 
65
132
  const input = { a: "four" }
66
133
 
67
- const { data, out } = t(input)
134
+ const out = t(input)
68
135
 
69
- attest<{ a: number } | undefined>(out).equals({ a: 4 })
70
- attest<{ a: string } | undefined>(data).snap({ a: "four" })
71
- // out is cloned before the morph
72
- attest(out === (input as never)).equals(false)
73
- // data has the original reference + data
74
- attest(data === input).equals(true)
136
+ attest<{ a: number } | type.errors>(out).equals({ a: 4 })
75
137
  })
76
138
 
77
139
  it("in array", () => {
78
140
  const types = scope({
79
- lengthOfString: ["string", "=>", (data) => data.length],
141
+ lengthOfString: ["string", "=>", data => data.length],
80
142
  mapToLengths: "lengthOfString[]"
81
143
  }).export()
82
144
  attest<Type<((In: string) => Out<number>)[]>>(types.mapToLengths)
83
- const { out } = types.mapToLengths(["1", "22", "333"])
84
- attest<number[] | undefined>(out).equals([1, 2, 3])
145
+ const out = types.mapToLengths(["1", "22", "333"])
146
+ attest<number[] | type.errors>(out).equals([1, 2, 3])
85
147
  })
86
148
 
87
149
  it("object to string", () => {
88
- const t = type([{ a: "string" }, "=>", (data) => JSON.stringify(data)])
89
- const { data, out } = t({ a: "foo" })
90
- attest<{ a: string } | undefined>(data).snap({ a: "foo" })
91
- attest<string | undefined>(out).snap('{"a":"foo"}')
150
+ const t = type([{ a: "string" }, "=>", data => JSON.stringify(data)])
151
+ const out = t({ a: "foo" })
152
+ attest<string | type.errors>(out).snap('{"a":"foo"}')
92
153
  })
93
154
 
94
155
  it("intersection", () => {
95
156
  const $ = scope({
96
157
  b: "3.14",
97
- a: ["number", "=>", (data) => `${data}`],
158
+ a: ["number", "=>", data => `${data}`],
98
159
  aAndB: () => $.type("a&b"),
99
160
  bAndA: () => $.type("b&a")
100
161
  })
101
162
  const types = $.export()
102
- assertNodeKind(types.bAndA, "morph")
103
- assertNodeKind(types.aAndB, "morph")
163
+ assertNodeKind(types.bAndA.raw, "morph")
164
+ assertNodeKind(types.aAndB.raw, "morph")
165
+
104
166
  attest<Type<(In: 3.14) => Out<string>>>(types.aAndB)
105
167
  attest(types.aAndB.json).snap({
106
- in: { unit: 3.14 },
107
- morphs: types.aAndB.serializedMorphs
168
+ from: { unit: 3.14 },
169
+ morphs: types.aAndB.raw.serializedMorphs
108
170
  })
109
171
  attest<typeof types.aAndB>(types.bAndA)
110
172
  attest(types.bAndA).equals(types.aAndB)
@@ -112,29 +174,29 @@ contextualize(() => {
112
174
 
113
175
  it("object intersection", () => {
114
176
  const $ = scope({
115
- a: [{ a: "1" }, "=>", (data) => `${data}`],
177
+ a: [{ a: "1" }, "=>", data => `${data}`],
116
178
  b: { b: "2" },
117
179
  c: "a&b"
118
180
  })
119
181
  const types = $.export()
120
182
  // TODO: FIX
121
183
  // attest<Type<(In: { a: 1; b: 2 }) => string>>(types.c)
122
- assertNodeKind(types.c, "morph")
184
+ assertNodeKind(types.c.raw, "morph")
123
185
  attest(types.c.json).snap({
124
- in: {
186
+ from: {
125
187
  domain: "object",
126
188
  prop: [
127
189
  { key: "a", value: { unit: 1 } },
128
190
  { key: "b", value: { unit: 2 } }
129
191
  ]
130
192
  },
131
- morphs: types.c.serializedMorphs
193
+ morphs: types.c.raw.serializedMorphs
132
194
  })
133
195
  })
134
196
 
135
197
  it("union", () => {
136
198
  const types = scope({
137
- a: ["number", "=>", (data) => `${data}`],
199
+ a: ["number", "=>", data => `${data}`],
138
200
  b: "boolean",
139
201
  aOrB: "a|b",
140
202
  bOrA: "b|a"
@@ -143,7 +205,7 @@ contextualize(() => {
143
205
  const serializedMorphs =
144
206
  types.aOrB.raw.firstReferenceOfKindOrThrow("morph").serializedMorphs
145
207
  attest(types.aOrB.json).snap([
146
- { in: "number", morphs: serializedMorphs },
208
+ { from: "number", morphs: serializedMorphs },
147
209
  { unit: false },
148
210
  { unit: true }
149
211
  ])
@@ -158,7 +220,7 @@ contextualize(() => {
158
220
 
159
221
  it("deep union", () => {
160
222
  const types = scope({
161
- a: { a: ["number>0", "=>", (data) => `${data}`] },
223
+ a: { a: ["number>0", "=>", data => `${data}`] },
162
224
  b: { a: "Function" },
163
225
  c: "a|b"
164
226
  }).export()
@@ -184,7 +246,7 @@ contextualize(() => {
184
246
  {
185
247
  key: "a",
186
248
  value: {
187
- in: {
249
+ from: {
188
250
  domain: "number",
189
251
  min: { exclusive: true, rule: 0 }
190
252
  },
@@ -198,77 +260,77 @@ contextualize(() => {
198
260
 
199
261
  it("chained reference", () => {
200
262
  const $ = scope({
201
- a: type("string").morph((s) => s.length),
202
- b: () => $.type("a").morph((n) => n === 0)
263
+ a: type("string").pipe(s => s.length),
264
+ b: () => $.type("a").pipe(n => n === 0)
203
265
  })
204
266
  const types = $.export()
205
267
  attest<Type<(In: string) => Out<boolean>>>(types.b)
206
- assertNodeKind(types.b, "morph")
268
+ assertNodeKind(types.b.raw, "morph")
207
269
  attest(types.b.json).snap({
208
- in: "string",
209
- morphs: types.b.serializedMorphs
270
+ from: "string",
271
+ morphs: types.b.raw.serializedMorphs
210
272
  })
211
273
  })
212
274
 
213
275
  it("chained nested", () => {
214
276
  const $ = scope({
215
- a: type("string").morph((s) => s.length),
216
- b: () => $.type({ a: "a" }).morph(({ a }) => a === 0)
277
+ a: type("string").pipe(s => s.length),
278
+ b: () => $.type({ a: "a" }).pipe(({ a }) => a === 0)
217
279
  })
218
280
 
219
281
  const types = $.export()
220
282
  attest<Type<(In: { a: string }) => Out<boolean>>>(types.b)
221
- assertNodeKind(types.b, "morph")
222
- assertNodeKind(types.a, "morph")
283
+ assertNodeKind(types.b.raw, "morph")
284
+ assertNodeKind(types.a.raw, "morph")
223
285
  attest(types.b.json).snap({
224
- in: {
286
+ from: {
225
287
  domain: "object",
226
288
  prop: [
227
289
  {
228
290
  key: "a",
229
291
  value: {
230
- in: "string",
231
- morphs: types.a.serializedMorphs
292
+ from: "string",
293
+ morphs: types.a.raw.serializedMorphs
232
294
  }
233
295
  }
234
296
  ]
235
297
  },
236
- morphs: types.b.serializedMorphs
298
+ morphs: types.b.raw.serializedMorphs
237
299
  })
238
300
  })
239
301
 
240
302
  it("directly nested", () => {
241
303
  const t = type([
242
304
  {
243
- a: ["string", "=>", (s) => s.length]
305
+ a: ["string", "=>", s => s.length]
244
306
  },
245
307
  "=>",
246
308
  ({ a }) => a === 0
247
309
  ])
248
310
  // TODO: check
249
311
  // attest<Type<(In: { a: string }) => Out<boolean>>>(t)
250
- assertNodeKind(t, "morph")
251
- const nestedMorph = t.firstReferenceOfKindOrThrow("morph")
312
+ assertNodeKind(t.raw, "morph")
313
+ const nestedMorph = t.raw.firstReferenceOfKindOrThrow("morph")
252
314
  attest(t.json).snap({
253
- in: {
315
+ from: {
254
316
  domain: "object",
255
317
  prop: [
256
318
  {
257
319
  key: "a",
258
320
  value: {
259
- in: "string",
321
+ from: "string",
260
322
  morphs: nestedMorph.serializedMorphs
261
323
  }
262
324
  }
263
325
  ]
264
326
  },
265
- morphs: t.serializedMorphs
327
+ morphs: t.raw.serializedMorphs
266
328
  })
267
329
  })
268
330
 
269
331
  it("discriminable tuple union", () => {
270
332
  const $ = scope({
271
- a: () => $.type(["string"]).morph((s) => [...s, "!"]),
333
+ a: () => $.type(["string"]).pipe(s => [...s, "!"]),
272
334
  b: ["boolean"],
273
335
  c: () => $.type("a|b")
274
336
  })
@@ -282,21 +344,20 @@ contextualize(() => {
282
344
  "=>",
283
345
  (s, ctx) => {
284
346
  const result = Number.parseInt(s)
285
- if (Number.isNaN(result)) {
286
- return ctx.error("an integer string")
287
- }
347
+ if (Number.isNaN(result)) return ctx.error("an integer string")
348
+
288
349
  return result
289
350
  }
290
351
  ])
291
352
  attest<Type<(In: string) => Out<number>>>(parsedInt)
292
- attest(parsedInt("5").out).snap(5)
293
- attest(parsedInt("five").errors?.summary).snap(
353
+ attest(parsedInt("5")).snap(5)
354
+ attest(parsedInt("five").toString()).snap(
294
355
  'must be an integer string (was "five")'
295
356
  )
296
357
  })
297
358
 
298
359
  it("nullable return", () => {
299
- const toNullableNumber = type(["string", "=>", (s) => s.length || null])
360
+ const toNullableNumber = type(["string", "=>", s => s.length || null])
300
361
  attest<Type<(In: string) => Out<number | null>>>(toNullableNumber)
301
362
  })
302
363
 
@@ -304,7 +365,7 @@ contextualize(() => {
304
365
  const toUndefinableNumber = type([
305
366
  "string",
306
367
  "=>",
307
- (s) => s.length || undefined
368
+ s => s.length || undefined
308
369
  ])
309
370
  attest<Type<(In: string) => Out<number | undefined>>>(toUndefinableNumber)
310
371
  })
@@ -313,7 +374,7 @@ contextualize(() => {
313
374
  const toMaybeNumber = type([
314
375
  "string",
315
376
  "=>",
316
- (s) =>
377
+ s =>
317
378
  s.length === 0 ? undefined
318
379
  : s.length === 1 ? null
319
380
  : s.length
@@ -53,22 +53,12 @@ contextualize(() => {
53
53
 
54
54
  it("infers input and output", () => {
55
55
  const $ = scope({
56
- a: ["string", "=>", (s) => s.length]
56
+ a: ["string", "=>", s => s.length]
57
57
  })
58
58
  attest<{ a: number }>($.infer)
59
- // TODO: API?
60
- // attest<{ a: string }>($.in.infer)
59
+
60
+ attest<{ a: string }>($.inferIn)
61
61
  })
62
- // TODO: remove if not preserving
63
- // it("scope.scope", () => {
64
- // const $ = scope({
65
- // a: "string"
66
- // })
67
- // const importer = $.scope({ b: "a[]" })
68
- // attest<{ b: string[] }>(importer.infer)
69
- // const t = importer.type("b")
70
- // attest(t.json).equals(type("string[]").json)
71
- // })
72
62
 
73
63
  it("infers its own helpers", () => {
74
64
  const $ = scope({
@@ -151,8 +141,8 @@ contextualize(() => {
151
141
  X: Apple
152
142
  }).export()
153
143
 
154
- const { out: data } = X({ pear: { tasty: true } })
155
- attest(data).snap({ pear: { tasty: true } })
144
+ const out = X({ pear: { tasty: true } })
145
+ attest(out).snap({ pear: { tasty: true } })
156
146
  })
157
147
  describe("define", () => {
158
148
  it("ark", () => {