arktype 2.2.10 → 3.0.0-beta.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/README.md +181 -15
- package/out/attributes.d.ts +50 -14
- package/out/checkers.d.ts +172 -0
- package/out/chunk-2U2AWCI3.js +626 -0
- package/out/chunk-4CUA5XIP.js +7 -0
- package/out/chunk-5U2ZMDNI.js +1566 -0
- package/out/chunk-DLWCO4RV.js +18 -0
- package/out/chunk-E3DZXEZK.js +72 -0
- package/out/chunk-EVCML554.js +47 -0
- package/out/chunk-G45P2CLE.js +63 -0
- package/out/chunk-GPNK5353.js +2529 -0
- package/out/chunk-MLKGABMK.js +9 -0
- package/out/chunk-NRIGTMPM.js +7 -0
- package/out/chunk-PRTMQCHS.js +3 -0
- package/out/chunk-R5G57XC7.js +7 -0
- package/out/chunk-RAZ3TXOE.js +313 -0
- package/out/chunk-SWOOCAHN.js +4 -0
- package/out/chunk-TAHPKDNM.js +3 -0
- package/out/chunk-UETB6YE2.js +14 -0
- package/out/chunk-UK5UGFOX.js +254 -0
- package/out/chunk-VV6YDN2D.js +6118 -0
- package/out/codecs.d.ts +21 -0
- package/out/codecs.js +7 -0
- package/out/config.d.ts +8 -3
- package/out/config.js +2 -3
- package/out/declare.d.ts +17 -3
- package/out/deferred.d.ts +10 -0
- package/out/errors.d.ts +1 -0
- package/out/errors.js +2 -0
- package/out/fn.d.ts +1 -1
- package/out/generic.d.ts +3 -3
- package/out/i18n.d.ts +2 -0
- package/out/i18n.js +2 -0
- package/out/index.d.ts +5 -4
- package/out/index.js +8 -9
- package/out/internal.d.ts +70 -0
- package/out/internal.js +17 -0
- package/out/island.d.ts +2 -0
- package/out/island.js +6 -0
- package/out/islandDependency.d.ts +2 -0
- package/out/islandDependency.js +18 -0
- package/out/keywords/Array.d.ts +2 -2
- package/out/keywords/builtins.d.ts +5 -3
- package/out/keywords/constructors.d.ts +9 -1
- package/out/keywords/functions.d.ts +254 -0
- package/out/keywords/keywords.d.ts +8 -9
- package/out/keywords/number.d.ts +2 -8
- package/out/keywords/phrases.d.ts +245 -0
- package/out/keywords/string.d.ts +14 -113
- package/out/keywords/ts.d.ts +35 -33
- package/out/keywords/ts.js +4 -82
- package/out/locale.d.ts +19 -0
- package/out/locales/de.d.ts +2 -0
- package/out/locales/de.js +2 -0
- package/out/locales/en.d.ts +4 -0
- package/out/locales/en.js +5 -0
- package/out/match.d.ts +6 -6
- package/out/module.d.ts +1 -1
- package/out/mutatesInput.js +3 -0
- package/out/parser/ast/generic.d.ts +6 -3
- package/out/parser/ast/infer.d.ts +1 -1
- package/out/parser/ast/validate.d.ts +11 -6
- package/out/parser/definition.d.ts +68 -7
- package/out/parser/objectLiteral.d.ts +40 -12
- package/out/parser/property.d.ts +3 -5
- package/out/parser/reduce/dynamic.d.ts +16 -7
- package/out/parser/reduce/shared.d.ts +4 -0
- package/out/parser/reduce/static.d.ts +13 -13
- package/out/parser/shift/operand/date.d.ts +0 -5
- package/out/parser/shift/operand/operand.d.ts +2 -1
- package/out/parser/shift/operand/unenclosed.d.ts +9 -6
- package/out/parser/shift/operand/unresolvable.d.ts +68 -0
- package/out/parser/shift/operator/bounds.d.ts +9 -4
- package/out/parser/shift/operator/brand.d.ts +2 -2
- package/out/parser/shift/operator/default.d.ts +2 -2
- package/out/parser/shift/operator/divisor.d.ts +2 -2
- package/out/parser/shift/operator/operator.d.ts +1 -1
- package/out/parser/shift/tokens.d.ts +9 -2
- package/out/parser/string.d.ts +61 -7
- package/out/parser/tupleExpressions.d.ts +7 -3
- package/out/precompiled.d.ts +1 -0
- package/out/precompiled.js +2 -0
- package/out/runtime.d.ts +181 -0
- package/out/runtime.js +4 -0
- package/out/runtimeDevelopment.d.ts +1 -0
- package/out/runtimeDevelopment.js +133 -0
- package/out/runtimeExports.d.ts +1 -0
- package/out/scope.d.ts +40 -11
- package/out/standalone/artifact.d.ts +265 -0
- package/out/standalone/baking.d.ts +201 -0
- package/out/standalone/combinators.d.ts +13 -0
- package/out/standalone/compilers.d.ts +23 -0
- package/out/standalone/creators.d.ts +41 -0
- package/out/standalone/fallbacks.d.ts +48 -0
- package/out/standalone/keys.d.ts +31 -0
- package/out/standalone/modules.d.ts +26 -0
- package/out/standalone/sites.d.ts +52 -0
- package/out/standalone/units.d.ts +31 -0
- package/out/standalone/values.d.ts +60 -0
- package/out/standalone.d.ts +16 -0
- package/out/type.d.ts +45 -6
- package/out/variants/array.d.ts +1 -1
- package/out/variants/base.d.ts +47 -27
- package/out/variants/date.d.ts +1 -1
- package/out/variants/instantiate.d.ts +4 -2
- package/out/variants/number.d.ts +1 -1
- package/out/variants/object.d.ts +17 -12
- package/out/variants/string.d.ts +1 -1
- package/package.json +169 -8
- package/skills/arktype/SKILL.md +278 -43
- package/skills/arktype/references/migrating.md +1296 -0
- package/skills/arktype/references/precompile.md +191 -0
- package/out/attributes.js +0 -3
- package/out/fn.js +0 -58
- package/out/generic.js +0 -37
- package/out/keywords/Array.js +0 -21
- package/out/keywords/FormData.js +0 -40
- package/out/keywords/TypedArray.js +0 -16
- package/out/keywords/builtins.js +0 -11
- package/out/keywords/constructors.js +0 -16
- package/out/keywords/keywords.js +0 -48
- package/out/keywords/number.js +0 -49
- package/out/keywords/string.js +0 -467
- package/out/match.js +0 -95
- package/out/module.js +0 -3
- package/out/nary.js +0 -1
- package/out/parser/ast/bounds.js +0 -1
- package/out/parser/ast/default.js +0 -1
- package/out/parser/ast/divisor.js +0 -1
- package/out/parser/ast/generic.js +0 -1
- package/out/parser/ast/infer.js +0 -1
- package/out/parser/ast/keyof.js +0 -1
- package/out/parser/ast/utils.js +0 -1
- package/out/parser/ast/validate.js +0 -3
- package/out/parser/definition.js +0 -78
- package/out/parser/objectLiteral.js +0 -113
- package/out/parser/property.js +0 -16
- package/out/parser/reduce/dynamic.js +0 -153
- package/out/parser/reduce/shared.js +0 -25
- package/out/parser/reduce/static.js +0 -1
- package/out/parser/shift/operand/date.js +0 -23
- package/out/parser/shift/operand/enclosed.js +0 -82
- package/out/parser/shift/operand/genericArgs.js +0 -16
- package/out/parser/shift/operand/operand.js +0 -16
- package/out/parser/shift/operand/unenclosed.js +0 -59
- package/out/parser/shift/operator/bounds.js +0 -91
- package/out/parser/shift/operator/brand.js +0 -6
- package/out/parser/shift/operator/default.js +0 -29
- package/out/parser/shift/operator/divisor.js +0 -13
- package/out/parser/shift/operator/operator.js +0 -28
- package/out/parser/shift/tokens.js +0 -37
- package/out/parser/string.js +0 -42
- package/out/parser/tupleExpressions.js +0 -75
- package/out/parser/tupleLiteral.js +0 -128
- package/out/scope.js +0 -106
- package/out/type.js +0 -55
- package/out/variants/array.js +0 -1
- package/out/variants/base.js +0 -1
- package/out/variants/date.js +0 -1
- package/out/variants/instantiate.js +0 -1
- package/out/variants/number.js +0 -1
- package/out/variants/object.js +0 -1
- package/out/variants/string.js +0 -1
- /package/out/{declare.js → mutatesInput.d.ts} +0 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# Precompiling ArkType
|
|
2
|
+
|
|
3
|
+
`@ark/aot` constructs each Type when the project builds and replaces it with a standalone validator that imports only `arktype/runtime`, so arktype's parser never ships. Inferred types are unchanged, as are most definitions; one built from values known only at runtime is rewritten as [below](#writing-definitions-the-build-accepts). Full docs: https://arktype.io/docs/precompile/llms.txt.
|
|
4
|
+
|
|
5
|
+
## Set up
|
|
6
|
+
|
|
7
|
+
1. Add `arktype` to `dependencies` and `@ark/aot` (Node 22.12 or later) to `devDependencies` with the project's package manager, e.g. `npm i arktype` and `npm i -D @ark/aot`. Keep one copy of `arktype`: the build fails if the program resolves two.
|
|
8
|
+
2. Add the plugin to the bundler, as below.
|
|
9
|
+
3. In a monorepo, a workspace package, like a shared `schemas` package, that pnpm, npm, Yarn or Bun workspaces link into `node_modules` resolves to its own directory, so the build precompiles it with the program; nothing extra is needed. Pass each such package's directory to `--check` too, e.g. `src ../../packages/schemas/src`.
|
|
10
|
+
4. If the project calls `configure()`, name its module as `config` in an `ark.json` beside `package.json`, as below, or under an `"ark"` key of the same shape in `package.json` (the build fails if both are set). Put build options there, not in the plugin call: the plugin, the test drivers, `arktype-precompile` and the ESLint rule all read `ark.json`, but a test driver doesn't see the plugin's options, so the tests would fail without them.
|
|
11
|
+
5. Make the tests run precompiled too, as below.
|
|
12
|
+
6. Run `npx arktype-precompile --check src`, which reads each JavaScript and TypeScript file in `src` and lists each definition the build would refuse, with the fix, until every site is accepted. Then run the build and the tests.
|
|
13
|
+
|
|
14
|
+
The ESLint rule reports the same errors as code is written: add `configs.recommended` from `@ark/aot/eslint` to `eslint.config.js`.
|
|
15
|
+
|
|
16
|
+
```json
|
|
17
|
+
{
|
|
18
|
+
"$schema": "./node_modules/@ark/aot/ark.schema.json",
|
|
19
|
+
"precompile": { "config": "./src/arktype.config.ts" }
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Bundlers
|
|
24
|
+
|
|
25
|
+
Vite, and Vitest, which runs tests through the same plugin:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
// vite.config.ts
|
|
29
|
+
import { precompile } from "@ark/aot/unplugin"
|
|
30
|
+
|
|
31
|
+
export default {
|
|
32
|
+
plugins: [precompile.vite()]
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Frameworks that build with Vite list it right after their own plugin and ahead of `react()`, a Babel plugin like the React Compiler's or another that rewrites modules:
|
|
37
|
+
|
|
38
|
+
- SvelteKit: `plugins: [sveltekit(), precompile.vite()]` in `vite.config.ts`
|
|
39
|
+
- TanStack Router: `plugins: [tanstackRouter(), precompile.vite(), react()]`, an order the build requires where it splits routes
|
|
40
|
+
- Astro: `vite: { plugins: [precompile.vite()] }` in `astro.config`
|
|
41
|
+
- Nuxt: `vite: { plugins: [precompile.vite()] }` and `nitro: { rollupConfig: { plugins: [precompile.rollup({ prune: process.env.NODE_ENV === "production" })] } }` in `nuxt.config.ts`, since Nitro bundles server routes and `shared/` itself with Rollup, which has no production mode to prune in
|
|
42
|
+
|
|
43
|
+
The build precompiles JavaScript and TypeScript modules, including an Astro `<script>`, but not a Svelte component's script or another file that isn't JavaScript or TypeScript: define the Types a component uses, with any function checking a result against `type.errors`, in a `.ts` module the component imports. A component may call a validator and read its `.allows` or `.assert`. Its other reads fail at `vite build`, not before, as `--check` reads only JavaScript and TypeScript.
|
|
44
|
+
|
|
45
|
+
esbuild needs `bundle: true`, and `precompile.esbuild()` ahead of any plugin that loads `.ts` files. A production build adds `conditions: ["production"]`, leaving out the checks validators make in development:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// build.ts
|
|
49
|
+
import { precompile } from "@ark/aot/unplugin"
|
|
50
|
+
import { build } from "esbuild"
|
|
51
|
+
|
|
52
|
+
await build({
|
|
53
|
+
entryPoints: ["src/main.ts"],
|
|
54
|
+
bundle: true,
|
|
55
|
+
platform: "node",
|
|
56
|
+
outfile: "dist/main.js",
|
|
57
|
+
plugins: [precompile.esbuild()]
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Others: `precompile.rollup()` (ahead of `@rollup/plugin-typescript`), `precompile.rolldown()`, `precompile.webpack()` (with ts-loader's `transpileOnly: true`, type-checking with `tsc --noEmit`), `precompile.rspack()`, `precompile.rsbuild()`, `precompile.farm()` and `precompile.bun()` for `Bun.build`, as `await Bun.build({ entrypoints: ["src/server.ts"], outdir: "dist", target: "bun", plugins: [precompile.bun()] })`, where `target: "bun"` builds for Bun rather than a browser. Snippets for each: https://arktype.io/docs/precompile/setup.md.
|
|
62
|
+
|
|
63
|
+
### Next.js
|
|
64
|
+
|
|
65
|
+
Next 16 builds with Turbopack. Export the config through `withPrecompile`, which reads the project before Turbopack starts so `next build` can prune. Add a loader rule for each extension holding a definition or the config module, and one for JavaScript mentioning arktype, so a dependency that defines Types fails the build rather than at runtime. Then alias arktype's entries to the runtime, and @ark/schema's runtime to the one the loader writes for your options:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
// next.config.js (or the same in next.config.ts)
|
|
69
|
+
import { withPrecompile } from "@ark/aot/turbopack"
|
|
70
|
+
|
|
71
|
+
export default withPrecompile({
|
|
72
|
+
// for the pages router
|
|
73
|
+
bundlePagesRouterDependencies: true,
|
|
74
|
+
turbopack: {
|
|
75
|
+
rules: {
|
|
76
|
+
"*.ts": { loaders: ["@ark/aot/turbopack"] },
|
|
77
|
+
"*.tsx": { loaders: ["@ark/aot/turbopack"] },
|
|
78
|
+
"*.{js,mjs}": {
|
|
79
|
+
condition: { content: /arktype/ },
|
|
80
|
+
loaders: ["@ark/aot/turbopack"]
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
resolveAlias: {
|
|
84
|
+
arktype: "arktype/runtime",
|
|
85
|
+
"arktype/codecs": "arktype/runtime",
|
|
86
|
+
"arktype/config": "arktype/runtime",
|
|
87
|
+
"arktype/errors": "arktype/runtime",
|
|
88
|
+
"arktype/mutatesInput": "arktype/runtime",
|
|
89
|
+
"@ark/schema/runtime": "@ark/aot/turbopack/runtime"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Next 15 builds with webpack, as does `next build --webpack`: push `precompile.webpack()` onto `config.plugins` in the `webpack` function instead.
|
|
96
|
+
|
|
97
|
+
On the pages router's server, Next otherwise leaves dependencies external, where the aliases don't reach, so a precompiled validator's errors fail `instanceof type.errors`. With either bundler, without `bundlePagesRouterDependencies: true` the build fails with `Next.js leaves the pages router's dependencies external on the server ...`. `transpilePackages: ["arktype"]` isn't enough: a dependency left external still loads its own copy of arktype. A package `serverExternalPackages` names that imports arktype fails the build too.
|
|
98
|
+
|
|
99
|
+
### Without a bundler
|
|
100
|
+
|
|
101
|
+
A project that `tsc` compiles to run from its `outDir` precompiles what it emits: `"build": "tsc && arktype-precompile --emit"`. `--emit` writes each module reading arktype again from its source, so run anything that rewrites `tsc`'s output, like `tsc-alias`, after it: `"tsc && arktype-precompile --emit && tsc-alias"`.
|
|
102
|
+
|
|
103
|
+
A library that others install doesn't precompile itself, with `--emit` or a bundler: an app's build fails for a dependency that imports `arktype/runtime`. Precompiling a library means publishing what `tsc` emits with `"ark": { "aot": true }` in its `package.json` and `arktype` in `peerDependencies`, so each app's build precompiles it, and running `npx arktype-precompile --check src` until it accepts every definition.
|
|
104
|
+
|
|
105
|
+
A package whose types others read writes its declarations with `npx arktype-precompile --declare` in place of `tsc`'s: each Type, Scope and Module is declared by its shape, in a `~arktype.d.ts` beside the others, and nothing changes at runtime. A cyclic scope's types ship no other way, since `tsc` declares an inferred cycle as `any` or fails with `TS5088`. It reads the program with the TypeScript installed: 5.5 to 6's compiler API, or TypeScript 7.1 or later's native API through tsgo, or tsrs where its binary is installed; `--checker tsjs|tsgo|tsrs` chooses one.
|
|
106
|
+
|
|
107
|
+
### Tests
|
|
108
|
+
|
|
109
|
+
- Vitest: nothing beyond the Vite plugin.
|
|
110
|
+
- `node --test`, or any Node program (Node 22.15 or later): `node --import ./register.ts --test`, or `node --import ./register.ts src/server.ts` to run the program, with this file beside `package.json` (as `register.js` where Node can't run TypeScript):
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// register.ts
|
|
114
|
+
import { register } from "@ark/aot/node"
|
|
115
|
+
|
|
116
|
+
register()
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
- Bun: `Bun.plugin(precompile())` with `precompile` from `@ark/aot/bun`, in a file listed under `preload` in `bunfig.toml`, and again under `[test]` for `bun test`.
|
|
120
|
+
- Jest has no driver, so its tests run on arktype, whose error messages are a precompiled validator's.
|
|
121
|
+
|
|
122
|
+
A project with a config module loads it before each test file, or the first validator a test loads throws that it was created before `configure()` ran: list it in Vitest's `test.setupFiles`, after the plugin in Bun's `[test]` preload, or after the Node driver: `node --import ./register.ts --import ./src/arktype.config.ts --test`.
|
|
123
|
+
|
|
124
|
+
To check that each precompiled validator behaves as arktype does, run the tests once with `precompile.vite({ shadow: true })`.
|
|
125
|
+
|
|
126
|
+
## Writing definitions the build accepts
|
|
127
|
+
|
|
128
|
+
The build evaluates each definition, so it must be built from values the build knows, and anything composing a Type must happen inside a definition.
|
|
129
|
+
|
|
130
|
+
A helper whose body is one expression, or consts and one `return`, like ``const text = (max: number) => type(`0 < string <= ${max}`)`` or `const nullable = <t>(T: Type<t>) => T.or("null")`, precompiles at each call passing it values the build can read, like `text(50)`, `text(LIMIT * 10)` or `nullable(Product)`. Call it directly: passed as a callback or held in an object, it fails the build, and a module the plugin doesn't transform, like a `.svelte` file, can't call it.
|
|
131
|
+
|
|
132
|
+
| Instead of | Write |
|
|
133
|
+
| -------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
|
134
|
+
| a helper called with a value known only at runtime, like a parameter | the call where that value is known, as `text(80)` |
|
|
135
|
+
| a shape object built or changed at runtime, then `type(shape)` | each variant as its own Type, chosen at runtime: `const Post = import.meta.env.DEV ? DevPost : BasePost` |
|
|
136
|
+
| composing at runtime, as in `(keys: Key[]) => User.pick(...keys)` | a Type for each case the program needs |
|
|
137
|
+
| `const Plan = T.default(x)`, then `{ plan: Plan }` | `plan: T.default(x)` at the key: `.default()` gives a key's definition, not a Type |
|
|
138
|
+
| `import("arktype")` or `require("arktype")` | an `import` declaration |
|
|
139
|
+
| a Zod or Valibot schema inside a definition | the schema converted to ArkType; until it is, build with `runtime: "arktype"` |
|
|
140
|
+
| a definition only known at runtime, like one loaded from a database | `// @ark-runtime` on the line before it, which ships arktype for that module |
|
|
141
|
+
|
|
142
|
+
Composing inside a definition is fine anywhere, even in a function body: `const Updates = StockUpdate.array().atLeastLength(1)` precompiles, as does `User.and({ role: "'admin'" })`. A read the build sees, like `Sku.description`, is replaced by its value.
|
|
143
|
+
|
|
144
|
+
## What changes at runtime
|
|
145
|
+
|
|
146
|
+
- A validator validates, `.assert()`s and is a Standard Schema, but has no `.and`, `.or`, `.pick` or other composing method. The build fails where it sees one called outside a definition.
|
|
147
|
+
- Each error in `out.issues` has only `code`, `path`, `data`, `expected` and `message`, and `arktype/runtime` doesn't export `byPath`, `flatByPath`, `flatMessagesByPath` or `messageIn`. To group messages by key, group `out.issues` by `issue.path.join(".")`.
|
|
148
|
+
- Error messages are arktype's: an issue's `message` says what failed, without its path or what the value was, like `must be an email address`, and `out.summary` writes the path before it, like `email must be an email address`. A missing key reads `name must be a string (was missing)`. Text configured with `.configure({ message })` or `.describe()` is kept, as is a `required` message configured in the config module, whose function receives `propString` and `valueMeta` as in arktype.
|
|
149
|
+
- The build works out which introspection members, like `toJsonSchema()`, `json` and `~standard.jsonSchema`, each validator needs, baking each where it sees code read it, following it into its dependencies and, in a build reading one module at a time, into each module importing it. So the MCP SDK, the AI SDK, Elysia's OpenAPI plugin, hono-openapi and oRPC's `@orpc/arktype` converter need nothing set, and setting one under `members` only overrides what the build works out. Where the build loses track of a validator, like one passed to a function it can't resolve, it bakes each into it and a production build warns `precompile: baking each introspection member into ...`; the build is correct, only larger. Code the build can't see, like code outside the bundle reading an entry's export, reads a member it didn't bake and throws, naming the member to set true under `members`. A production build decides `assert`, `meta`, `~standard` and `methods` the same way, keeping in each validator what the code it reaches reads, like `~standard` for Hono's `sValidator`, with nothing to set. tRPC, Hono's validators, React Hook Form, TanStack Form and Router and t3-env need nothing.
|
|
150
|
+
- `drizzle-arktype` creates Types from your tables as the program runs, so it runs on arktype, as the next item sets, which ships the parser. To precompile those Types instead, write each one out with a line that fails to compile if it drifts from the table, like `const matches: Equals<typeof selectTask.infer, typeof tasks.$inferSelect> = true`, with `Equals` from migrating.md's How to convert.
|
|
151
|
+
- A dependency that imports arktype fails a standalone build unless it opts in with `"ark": { "aot": true }` in its `package.json`, or the program decides for it under `"precompile"`: `"dependencies": { "@t3-oss/env-core": "aot" }` precompiles one whose Types are static, like t3-env's presets, and `"dependencies": { "drizzle-arktype": "arktype" }` runs one creating Types from values it reads as it runs on arktype, sharing the program's `configure()`. A dependency that only reads `type.errors`, like `@hono/arktype-validator`, builds as it is.
|
|
152
|
+
- On a server, Vite leaves dependencies external, so a build fails for one that imports arktype with `The bundle leaves my-lib, which imports arktype ...`: add it to `ssr.noExternal`, or `dependencies` if it defines Types. Only a build checks this, and only for a package declaring arktype: `vite dev`, Vitest and `Bun.build`'s `external` stay silent.
|
|
153
|
+
- A library you publish ships what `tsc` emits, not a precompiled build, with `"ark": { "aot": true }` in its `package.json` and arktype in `peerDependencies`, so each app's build precompiles it with the app's arktype. Run `--check` on its source before publishing, or a refusal surfaces only in an app's build.
|
|
154
|
+
- Locales from `arktype/i18n` aren't supported.
|
|
155
|
+
|
|
156
|
+
## Errors and their fixes
|
|
157
|
+
|
|
158
|
+
Each build error names the file, line and column, then why, then the fix on its last line. Apply that fix. A bundler stops at the first error, so run `npx arktype-precompile --check src` to see them all. Don't set `allowUnsafe` to get past one, and don't switch to `runtime: "arktype"` unless the program really composes or introspects Types at runtime, as that ships arktype's parser again.
|
|
159
|
+
|
|
160
|
+
| The build says | Fix |
|
|
161
|
+
| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
|
|
162
|
+
| `` `limit` is a parameter of makeField ...; its value is known only at runtime `` | call the helper where that value is known, with it; see the table above |
|
|
163
|
+
| `` `shape` is a parameter of createFinalSchema ..., which nothing calls by name; ... `` | mark the definition `// @ark-runtime`: only arktype sees what the caller, like a library, passes it |
|
|
164
|
+
| `T.or reads or, which a standalone validator lacks` | compose inside a definition, make the helper one expression, or consts and one `return`, or make it a Generic |
|
|
165
|
+
| `` `text` (...) is not a static helper: it runs `if (...)` ... `` | rewrite the helper's body as one expression, or consts and one `return` |
|
|
166
|
+
| `` `text` is read outside a call the build precompiles `` | call the helper directly, with arguments the build can read |
|
|
167
|
+
| `... exports text, a helper the build precompiles at each call, but ... imports it` | call the helper in a `.ts` or `.js` module and import what it returns |
|
|
168
|
+
| `` `shape` is written at ..., so its value at the site is not known `` | write the object whole in its literal, or define each variant as its own Type |
|
|
169
|
+
| `User.description reads description, which a standalone validator lacks` | read it inside a definition, or set that member under `members` |
|
|
170
|
+
| `T.default(x) can't be precompiled yet`, `.default() gives the definition of an object's key` | write `.default(x)` at the object key it's for, not in a `const`, as the message shows |
|
|
171
|
+
| `flatMessagesByPath(out) calls flatMessagesByPath, which arktype/runtime doesn't export` (or `byPath`, `flatByPath`) | read `out.issues`, grouping by `issue.path.join(".")`; for `messageIn`, read `issue.message` |
|
|
172
|
+
| `configure() is called, but the build has no config module` | add `"config"` to `ark.json`, as above |
|
|
173
|
+
| `... loads src/schemas.ts, which creates a Type, before the config module ... runs` | import the config module first in that file, before any module that creates a Type |
|
|
174
|
+
| `` `User` comes from `~/schemas`, which the build can't resolve `` | add the alias to `tsconfig.json`'s `paths` |
|
|
175
|
+
| `arktype resolves to more than one copy`, or a version mismatch | install one version of `arktype`, then clear the bundler's cache |
|
|
176
|
+
| `my-lib imports arktype, which a standalone build resolves to arktype/runtime` | set `"precompile": { "dependencies": { "my-lib": "aot" } }`, or `"arktype"` in place of `"aot"` if it creates Types from runtime values |
|
|
177
|
+
| `src/Form.svelte imports arktype, which a standalone build resolves to arktype/runtime` | move the Types, and any code reading `type`, into a `.ts` module the component imports; don't set `allowUnsafe` |
|
|
178
|
+
| `precompile.esbuild() needs bundle: true` | set `bundle: true` |
|
|
179
|
+
| `Next.js leaves the pages router's dependencies external on the server ...` | add `bundlePagesRouterDependencies: true` to the Next config |
|
|
180
|
+
| `The bundle leaves my-lib, which imports arktype ...` | add the package to `ssr.noExternal` (Vite on a server), or bundle it |
|
|
181
|
+
| `The build's serverExternalPackages option leaves my-lib ...` (Turbopack) | remove the package from `serverExternalPackages` |
|
|
182
|
+
| `arktype resolves to ... rather than arktype/runtime` (Turbopack) | add the `resolveAlias` entries it lists |
|
|
183
|
+
| `type is not a function` at runtime | a module defining Types wasn't precompiled, e.g. no Turbopack rule matches its extension; add one |
|
|
184
|
+
|
|
185
|
+
Three messages concern `members`:
|
|
186
|
+
|
|
187
|
+
- `prune: keeping assert, meta, ~standard and methods, because the validator at src/schemas.ts:7:9 is lost where ...` (a warning): the build kept the members it couldn't prove unread, as where a Type is passed to a function it can't resolve; the build is correct, only larger. To build without the warning, set each member it names under `members` in `ark.json`, `true` where code may read it, like `~standard` where a library validates with it, else `false`, e.g. `"precompile": { "members": { "assert": false, "meta": false, "~standard": true, "methods": false } }`.
|
|
188
|
+
- `toJsonSchema isn't precompiled`, at runtime in development, or `toJsonSchema is not a function` in production, where a member no build baked reads `undefined`: code the build can't see read it, like code outside the bundle reading an entry's export; set that member true under `members`.
|
|
189
|
+
- `some-plugin transformed src/form.ts after arktype's plugin read it, and the program as built reads what the build left out of its validators: a module reads assert of a validator` (Vite, Rollup or Rolldown; webpack and Rspack say it of a loader): a plugin after arktype's added a read of something a production build left out, and the build failed rather than ship it. Set each member the message names true under `members`, or `prune: false` where it names none. Moving the plugin doesn't help.
|
|
190
|
+
|
|
191
|
+
Every message and its fix: https://arktype.io/docs/precompile/troubleshooting.md.
|
package/out/attributes.js
DELETED
package/out/fn.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Callable, throwParseError } from "@ark/util";
|
|
2
|
-
export class InternalFnParser extends Callable {
|
|
3
|
-
constructor($) {
|
|
4
|
-
const parse = (...signature) => {
|
|
5
|
-
const returnOperatorIndex = signature.indexOf(":");
|
|
6
|
-
const lastParamIndex = returnOperatorIndex === -1 ?
|
|
7
|
-
signature.length - 1
|
|
8
|
-
: returnOperatorIndex - 1;
|
|
9
|
-
const paramDefs = signature.slice(0, lastParamIndex + 1);
|
|
10
|
-
const paramTuple = $.parse(paramDefs).assertHasKind("intersection");
|
|
11
|
-
let returnType = $.intrinsic.unknown;
|
|
12
|
-
if (returnOperatorIndex !== -1) {
|
|
13
|
-
if (returnOperatorIndex !== signature.length - 2)
|
|
14
|
-
return throwParseError(badFnReturnTypeMessage);
|
|
15
|
-
returnType = $.parse(signature[returnOperatorIndex + 1]);
|
|
16
|
-
}
|
|
17
|
-
return (impl) => new InternalTypedFn(impl, paramTuple, returnType);
|
|
18
|
-
};
|
|
19
|
-
// `raw` is an alias of `fn` itself with no type-level validation. It must
|
|
20
|
-
// reference `parse` directly rather than `$.fn`, which is still being
|
|
21
|
-
// constructed (and thus `undefined`) at this point.
|
|
22
|
-
const attach = {
|
|
23
|
-
$: $,
|
|
24
|
-
raw: parse
|
|
25
|
-
};
|
|
26
|
-
super(parse, { attach });
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
export class InternalTypedFn extends Callable {
|
|
30
|
-
raw;
|
|
31
|
-
params;
|
|
32
|
-
returns;
|
|
33
|
-
expression;
|
|
34
|
-
constructor(raw, params, returns) {
|
|
35
|
-
const typedName = `typed ${raw.name}`;
|
|
36
|
-
const typed = {
|
|
37
|
-
// assign to a key with the expected name to force it to be created that way
|
|
38
|
-
[typedName]: (...args) => {
|
|
39
|
-
const validatedArgs = params.assert(args);
|
|
40
|
-
const returned = raw(...validatedArgs);
|
|
41
|
-
return returns.assert(returned);
|
|
42
|
-
}
|
|
43
|
-
}[typedName];
|
|
44
|
-
super(typed);
|
|
45
|
-
this.raw = raw;
|
|
46
|
-
this.params = params;
|
|
47
|
-
this.returns = returns;
|
|
48
|
-
let argsExpression = params.expression;
|
|
49
|
-
if (argsExpression[0] === "[" &&
|
|
50
|
-
argsExpression[argsExpression.length - 1] === "]")
|
|
51
|
-
argsExpression = argsExpression.slice(1, -1);
|
|
52
|
-
else if (argsExpression.endsWith("[]"))
|
|
53
|
-
argsExpression = `...${argsExpression}`;
|
|
54
|
-
this.expression = `(${argsExpression}) => ${returns?.expression ?? "unknown"}`;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
export const badFnReturnTypeMessage = `":" must be followed by exactly one return type e.g:
|
|
58
|
-
fn("string", ":", "number")(s => s.length)`;
|
package/out/generic.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { GenericRoot } from "@ark/schema";
|
|
2
|
-
import { throwParseError } from "@ark/util";
|
|
3
|
-
import { RuntimeState } from "./parser/reduce/dynamic.js";
|
|
4
|
-
import { terminatingChars } from "./parser/shift/tokens.js";
|
|
5
|
-
import { parseUntilFinalizer } from "./parser/string.js";
|
|
6
|
-
export const Generic = GenericRoot;
|
|
7
|
-
export const emptyGenericParameterMessage = "An empty string is not a valid generic parameter name";
|
|
8
|
-
export const parseGenericParamName = (scanner, result, ctx) => {
|
|
9
|
-
scanner.shiftUntilNonWhitespace();
|
|
10
|
-
const name = scanner.shiftUntilLookahead(terminatingChars);
|
|
11
|
-
if (name === "") {
|
|
12
|
-
// if we've reached the end of the string and have parsed at least one
|
|
13
|
-
// param, return the valid result
|
|
14
|
-
if (scanner.lookahead === "" && result.length)
|
|
15
|
-
return result;
|
|
16
|
-
return throwParseError(emptyGenericParameterMessage);
|
|
17
|
-
}
|
|
18
|
-
scanner.shiftUntilNonWhitespace();
|
|
19
|
-
return _parseOptionalConstraint(scanner, name, result, ctx);
|
|
20
|
-
};
|
|
21
|
-
const extendsToken = "extends ";
|
|
22
|
-
const _parseOptionalConstraint = (scanner, name, result, ctx) => {
|
|
23
|
-
scanner.shiftUntilNonWhitespace();
|
|
24
|
-
if (scanner.unscanned.startsWith(extendsToken))
|
|
25
|
-
scanner.jumpForward(extendsToken.length);
|
|
26
|
-
else {
|
|
27
|
-
// if we don't have a contraining token here, return now so we can
|
|
28
|
-
// assume in the rest of the function body we do have a constraint
|
|
29
|
-
if (scanner.lookahead === ",")
|
|
30
|
-
scanner.shift();
|
|
31
|
-
result.push(name);
|
|
32
|
-
return parseGenericParamName(scanner, result, ctx);
|
|
33
|
-
}
|
|
34
|
-
const s = parseUntilFinalizer(new RuntimeState(scanner, ctx));
|
|
35
|
-
result.push([name, s.root]);
|
|
36
|
-
return parseGenericParamName(scanner, result, ctx);
|
|
37
|
-
};
|
package/out/keywords/Array.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { genericNode, intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
-
import { Hkt, liftArray } from "@ark/util";
|
|
3
|
-
import { Scope } from "../scope.js";
|
|
4
|
-
class liftFromHkt extends Hkt {
|
|
5
|
-
}
|
|
6
|
-
const liftFrom = genericNode("element")(args => {
|
|
7
|
-
const nonArrayElement = args.element.exclude(intrinsic.Array);
|
|
8
|
-
const lifted = nonArrayElement.array();
|
|
9
|
-
return nonArrayElement
|
|
10
|
-
.rawOr(lifted)
|
|
11
|
-
.pipe(liftArray)
|
|
12
|
-
.distribute(branch => branch.assertHasKind("morph").declareOut(lifted), rootSchema);
|
|
13
|
-
}, liftFromHkt);
|
|
14
|
-
export const arkArray = Scope.module({
|
|
15
|
-
root: intrinsic.Array,
|
|
16
|
-
readonly: "root",
|
|
17
|
-
index: intrinsic.nonNegativeIntegerString,
|
|
18
|
-
liftFrom
|
|
19
|
-
}, {
|
|
20
|
-
name: "Array"
|
|
21
|
-
});
|
package/out/keywords/FormData.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { rootSchema } from "@ark/schema";
|
|
2
|
-
import { registry } from "@ark/util";
|
|
3
|
-
import { Scope } from "../scope.js";
|
|
4
|
-
const value = rootSchema(["string", registry.FileConstructor]);
|
|
5
|
-
const parsedFormDataValue = value.rawOr(value.array());
|
|
6
|
-
const parsed = rootSchema({
|
|
7
|
-
meta: "an object representing parsed form data",
|
|
8
|
-
domain: "object",
|
|
9
|
-
index: {
|
|
10
|
-
signature: "string",
|
|
11
|
-
value: parsedFormDataValue
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
export const arkFormData = Scope.module({
|
|
15
|
-
root: ["instanceof", FormData],
|
|
16
|
-
value,
|
|
17
|
-
parsed,
|
|
18
|
-
parse: rootSchema({
|
|
19
|
-
in: FormData,
|
|
20
|
-
morphs: (data) => {
|
|
21
|
-
const result = {};
|
|
22
|
-
for (const [k, v] of data) {
|
|
23
|
-
if (k in result) {
|
|
24
|
-
const existing = result[k];
|
|
25
|
-
if (typeof existing === "string" ||
|
|
26
|
-
existing instanceof registry.FileConstructor)
|
|
27
|
-
result[k] = [existing, v];
|
|
28
|
-
else
|
|
29
|
-
existing.push(v);
|
|
30
|
-
}
|
|
31
|
-
else
|
|
32
|
-
result[k] = v;
|
|
33
|
-
}
|
|
34
|
-
return result;
|
|
35
|
-
},
|
|
36
|
-
declaredOut: parsed
|
|
37
|
-
})
|
|
38
|
-
}, {
|
|
39
|
-
name: "FormData"
|
|
40
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Scope } from "../scope.js";
|
|
2
|
-
export const TypedArray = Scope.module({
|
|
3
|
-
Int8: ["instanceof", Int8Array],
|
|
4
|
-
Uint8: ["instanceof", Uint8Array],
|
|
5
|
-
Uint8Clamped: ["instanceof", Uint8ClampedArray],
|
|
6
|
-
Int16: ["instanceof", Int16Array],
|
|
7
|
-
Uint16: ["instanceof", Uint16Array],
|
|
8
|
-
Int32: ["instanceof", Int32Array],
|
|
9
|
-
Uint32: ["instanceof", Uint32Array],
|
|
10
|
-
Float32: ["instanceof", Float32Array],
|
|
11
|
-
Float64: ["instanceof", Float64Array],
|
|
12
|
-
BigInt64: ["instanceof", BigInt64Array],
|
|
13
|
-
BigUint64: ["instanceof", BigUint64Array]
|
|
14
|
-
}, {
|
|
15
|
-
name: "TypedArray"
|
|
16
|
-
});
|
package/out/keywords/builtins.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { genericNode, intrinsic } from "@ark/schema";
|
|
2
|
-
import { Hkt } from "@ark/util";
|
|
3
|
-
import { Scope } from "../scope.js";
|
|
4
|
-
class MergeHkt extends Hkt {
|
|
5
|
-
description = 'merge an object\'s properties onto another like `Merge(User, { isAdmin: "true" })`';
|
|
6
|
-
}
|
|
7
|
-
const Merge = genericNode(["base", intrinsic.object], ["props", intrinsic.object])(args => args.base.merge(args.props), MergeHkt);
|
|
8
|
-
export const arkBuiltins = Scope.module({
|
|
9
|
-
Key: intrinsic.key,
|
|
10
|
-
Merge
|
|
11
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ecmascriptConstructors, flatMorph, platformConstructors } from "@ark/util";
|
|
2
|
-
import { Scope } from "../scope.js";
|
|
3
|
-
import { arkArray } from "./Array.js";
|
|
4
|
-
import { arkFormData } from "./FormData.js";
|
|
5
|
-
import { TypedArray } from "./TypedArray.js";
|
|
6
|
-
const omittedPrototypes = {
|
|
7
|
-
Boolean: 1,
|
|
8
|
-
Number: 1,
|
|
9
|
-
String: 1
|
|
10
|
-
};
|
|
11
|
-
export const arkPrototypes = Scope.module({
|
|
12
|
-
...flatMorph({ ...ecmascriptConstructors, ...platformConstructors }, (k, v) => (k in omittedPrototypes ? [] : [k, ["instanceof", v]])),
|
|
13
|
-
Array: arkArray,
|
|
14
|
-
TypedArray,
|
|
15
|
-
FormData: arkFormData
|
|
16
|
-
});
|
package/out/keywords/keywords.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { $arkTypeRegistry, scope } from "../scope.js";
|
|
2
|
-
import { arkBuiltins } from "./builtins.js";
|
|
3
|
-
import { arkPrototypes } from "./constructors.js";
|
|
4
|
-
import { number } from "./number.js";
|
|
5
|
-
import { string } from "./string.js";
|
|
6
|
-
import { arkTsGenerics, arkTsKeywords, object, unknown } from "./ts.js";
|
|
7
|
-
export const ark = scope({
|
|
8
|
-
...arkTsKeywords,
|
|
9
|
-
...arkTsGenerics,
|
|
10
|
-
...arkPrototypes,
|
|
11
|
-
...arkBuiltins,
|
|
12
|
-
string,
|
|
13
|
-
number,
|
|
14
|
-
object,
|
|
15
|
-
unknown
|
|
16
|
-
}, { prereducedAliases: true, name: "ark" });
|
|
17
|
-
export const keywords = ark.export();
|
|
18
|
-
Object.assign($arkTypeRegistry.ambient, keywords);
|
|
19
|
-
$arkTypeRegistry.typeAttachments = {
|
|
20
|
-
string: keywords.string.root,
|
|
21
|
-
number: keywords.number.root,
|
|
22
|
-
bigint: keywords.bigint,
|
|
23
|
-
boolean: keywords.boolean,
|
|
24
|
-
symbol: keywords.symbol,
|
|
25
|
-
undefined: keywords.undefined,
|
|
26
|
-
null: keywords.null,
|
|
27
|
-
object: keywords.object.root,
|
|
28
|
-
unknown: keywords.unknown.root,
|
|
29
|
-
false: keywords.false,
|
|
30
|
-
true: keywords.true,
|
|
31
|
-
never: keywords.never,
|
|
32
|
-
arrayIndex: keywords.Array.index,
|
|
33
|
-
Key: keywords.Key,
|
|
34
|
-
Record: keywords.Record,
|
|
35
|
-
Array: keywords.Array.root,
|
|
36
|
-
Date: keywords.Date
|
|
37
|
-
};
|
|
38
|
-
export const type = Object.assign(ark.type,
|
|
39
|
-
// assign attachments newly parsed in keywords
|
|
40
|
-
// future scopes add these directly from the
|
|
41
|
-
// registry when their TypeParsers are instantiated
|
|
42
|
-
$arkTypeRegistry.typeAttachments);
|
|
43
|
-
export const match = ark.match;
|
|
44
|
-
export const fn = ark.fn;
|
|
45
|
-
export const generic = ark.generic;
|
|
46
|
-
export const schema = ark.schema;
|
|
47
|
-
export const define = ark.define;
|
|
48
|
-
export const declare = ark.declare;
|
package/out/keywords/number.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
-
import { Scope } from "../scope.js";
|
|
3
|
-
/**
|
|
4
|
-
* As per the ECMA-262 specification:
|
|
5
|
-
* A time value supports a slightly smaller range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds.
|
|
6
|
-
*
|
|
7
|
-
* @see https://262.ecma-international.org/15.0/index.html#sec-time-values-and-time-range
|
|
8
|
-
*/
|
|
9
|
-
export const epoch = rootSchema({
|
|
10
|
-
domain: {
|
|
11
|
-
domain: "number",
|
|
12
|
-
meta: "a number representing a Unix timestamp"
|
|
13
|
-
},
|
|
14
|
-
divisor: {
|
|
15
|
-
rule: 1,
|
|
16
|
-
meta: `an integer representing a Unix timestamp`
|
|
17
|
-
},
|
|
18
|
-
min: {
|
|
19
|
-
rule: -8640000000000000,
|
|
20
|
-
meta: `a Unix timestamp after -8640000000000000`
|
|
21
|
-
},
|
|
22
|
-
max: {
|
|
23
|
-
rule: 8640000000000000,
|
|
24
|
-
meta: "a Unix timestamp before 8640000000000000"
|
|
25
|
-
},
|
|
26
|
-
meta: "an integer representing a safe Unix timestamp"
|
|
27
|
-
});
|
|
28
|
-
export const integer = rootSchema({
|
|
29
|
-
domain: "number",
|
|
30
|
-
divisor: 1
|
|
31
|
-
});
|
|
32
|
-
export const number = Scope.module({
|
|
33
|
-
root: intrinsic.number,
|
|
34
|
-
integer,
|
|
35
|
-
epoch,
|
|
36
|
-
safe: rootSchema({
|
|
37
|
-
domain: {
|
|
38
|
-
domain: "number",
|
|
39
|
-
numberAllowsNaN: false
|
|
40
|
-
},
|
|
41
|
-
min: Number.MIN_SAFE_INTEGER,
|
|
42
|
-
max: Number.MAX_SAFE_INTEGER
|
|
43
|
-
}),
|
|
44
|
-
NaN: ["===", Number.NaN],
|
|
45
|
-
Infinity: ["===", Number.POSITIVE_INFINITY],
|
|
46
|
-
NegativeInfinity: ["===", Number.NEGATIVE_INFINITY]
|
|
47
|
-
}, {
|
|
48
|
-
name: "number"
|
|
49
|
-
});
|