Skip to content

Commit 5812c32

Browse files
authored
improve type error for trailing finalizer (#1342)
1 parent c4589a1 commit 5812c32

File tree

12 files changed

+32
-59
lines changed

12 files changed

+32
-59
lines changed

ark/attest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ark/attest",
3-
"version": "0.44.0",
3+
"version": "0.44.1",
44
"license": "MIT",
55
"author": {
66
"name": "David Blass",

ark/docs/content/docs/expressions/index.mdx

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -417,40 +417,6 @@ const parseEvenSugared = type("string.numeric.parse", "|>", "number % 2")
417417

418418
</SyntaxTabs>
419419

420-
### Morph
421-
422-
Morphs allow you to transform your data after it is validated. You can read more about them in [their intro section](/docs/intro/morphs-and-more/).
423-
424-
<SyntaxTabs>
425-
<SyntaxTab fluent>
426-
427-
```ts
428-
// hover to see how morphs are represented at a type-level
429-
const trimStringStart = type("string").pipe(str => str.trimStart())
430-
```
431-
432-
</SyntaxTab>
433-
434-
<SyntaxTab tuple>
435-
436-
```ts
437-
// hover to see how morphs are represented at a type-level
438-
const trimStringStart = type(["string", "=>", str => str.trimStart()])
439-
```
440-
441-
</SyntaxTab>
442-
443-
<SyntaxTab args>
444-
445-
```ts
446-
// hover to see how morphs are represented at a type-level
447-
const trimStringStart = type("string", "=>", str => str.trimStart())
448-
```
449-
450-
</SyntaxTab>
451-
452-
</SyntaxTabs>
453-
454420
### Unit
455421

456422
While embedded [literal syntax](/docs/primitives#number-literals) is usually ideal for defining exact primitive values, `===` and `type.unit` can be helpful for referencing a non-serialiazable value like a `symbol` from your type.

ark/fs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ark/fs",
3-
"version": "0.44.0",
3+
"version": "0.44.1",
44
"license": "MIT",
55
"author": {
66
"name": "David Blass",

ark/repo/scratch.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { type } from "arktype"
22

3-
const t = type({
4-
/** FOO */
5-
foo: "string",
6-
/** BAR */
7-
bar: "number?"
3+
export const urDOOMed = type({
4+
grouping: "(0 | (1 | (2 | (3 | (4 | 5)[])[])[])[])[]",
5+
nestedGenerics: "Exclude<0n | unknown[] | Record<string, unknown>, object>",
6+
"escapes\\?": "'a | b' | 'c | d'"
87
})
9-
10-
const out = t.assert({ foo: "foo" })
11-
12-
out.foo
13-
out.bar

ark/schema/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ark/schema",
3-
"version": "0.44.0",
3+
"version": "0.44.1",
44
"license": "MIT",
55
"author": {
66
"name": "David Blass",

ark/type/__tests__/enclosed.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ contextualize(() => {
4646
})
4747

4848
it("double-quoted", () => {
49-
attest<"goodbye">(type('"goodbye"').infer)
49+
const t = type('"goodbye"')
50+
attest<"goodbye">(t.infer)
51+
attest(t.expression).snap('"goodbye"')
5052
})
5153

5254
it("regex literal", () => {
53-
attest<string>(type("/.*/").infer)
55+
const t = type("/.*/")
56+
attest<string>(t.infer)
57+
attest(t.expression).snap("string /.*/")
5458
})
5559

5660
it("invalid regex", () => {
@@ -60,21 +64,30 @@ contextualize(() => {
6064
})
6165

6266
it("mixed quote types", () => {
63-
attest<"'single-quoted'">(type(`"'single-quoted'"`).infer)
64-
attest<'"double-quoted"'>(type(`'"double-quoted"'`).infer)
67+
const t = type(`"'single-quoted'"`)
68+
attest<"'single-quoted'">(t.infer)
69+
attest(t.expression).snap("\"'single-quoted'\"")
70+
71+
const u = type(`'"double-quoted"'`)
72+
attest<'"double-quoted"'>(u.infer)
6573
})
6674

6775
it("ignores enclosed operators", () => {
68-
attest<"yes|no|maybe">(type("'yes|no|maybe'").infer)
76+
const t = type("'yes|no|maybe'")
77+
attest<"yes|no|maybe">(t.infer)
78+
attest(t.expression).snap('"yes|no|maybe"')
6979
})
7080

7181
it("mix of enclosed and unenclosed operators", () => {
72-
attest<"yes|no" | "true|false">(type("'yes|no'|'true|false'").infer)
82+
const t = type("'yes|no'|'true|false'")
83+
attest<"yes|no" | "true|false">(t.infer)
84+
attest(t.expression).snap('"true|false" | "yes|no"')
7385
})
7486

7587
it("escaped enclosing", () => {
7688
const t = type("'don\\'t'")
7789
attest<"don't">(t.infer)
90+
attest(t.expression).snap('"don\'t"')
7891
})
7992

8093
it("string literal stress", () => {

ark/type/__tests__/generic.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ contextualize(() => {
622622
}
623623
}).export()
624624
).type.errors.snap(
625-
'Type \'"nest"\' is not assignable to type \'"Unexpectedly failed to parse the expression resulting from ... " & { ast: GenericAst<[["t", unknown]], { readonly nest: "nest"; }, "$", "$">; }\'.Type \'"nest"\' is not assignable to type \'"Unexpectedly failed to parse the expression resulting from ... "\'.'
625+
'Type \'"nest"\' is not assignable to type \'"Failed to parse the expression resulting from ... " & { ast: GenericAst<[["t", unknown]], { readonly nest: "nest"; }, "$", "$">; }\'.Type \'"nest"\' is not assignable to type \'"Failed to parse the expression resulting from ... "\'.'
626626
)
627627
})
628628
})

ark/type/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "arktype",
33
"description": "Optimized runtime validation for TypeScript syntax",
4-
"version": "2.1.5",
4+
"version": "2.1.6",
55
"license": "MIT",
66
"repository": {
77
"type": "git",

ark/type/parser/ast/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export type validateAst<ast, $, args> =
6464
}
6565

6666
type writeUnexpectedExpressionMessage<expression extends string> =
67-
`Unexpectedly failed to parse the expression resulting from ${expression}`
67+
`Failed to parse the expression resulting from ${expression}`
6868

6969
export const writePrefixedPrivateReferenceMessage = <name extends string>(
7070
name: name

ark/type/parser/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ export type extractFinalizedResult<s extends StaticState> =
114114
: s["finalizer"] extends ErrorMessage ? s["finalizer"]
115115
: s["finalizer"] extends "?" ? [s["root"], "?"]
116116
: s["finalizer"] extends "=" ? parseDefault<s["root"], s["unscanned"]>
117-
: state.error<writeUnexpectedCharacterMessage<`${s["finalizer"]}`>>
117+
: ErrorMessage<writeUnexpectedCharacterMessage<s["finalizer"] & string>>

0 commit comments

Comments
 (0)