Skip to content

Commit

Permalink
fix: global config resolution, some inference (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad authored Jan 29, 2025
1 parent bfb869e commit bb5fbf2
Show file tree
Hide file tree
Showing 39 changed files with 630 additions and 540 deletions.
2 changes: 1 addition & 1 deletion ark/attest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ark/attest",
"version": "0.41.1",
"version": "0.41.2",
"license": "MIT",
"author": {
"name": "David Blass",
Expand Down
18 changes: 13 additions & 5 deletions ark/docs/content/docs/configuration/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ However, it's equally important that when you need different behavior, you can e
<td>**global**</td>
<td>all Types parsed after the config is applied</td>
<td>
```ts
```ts title="config.ts"
import { configure } from "arktype/config"
// importing from "arktype/config" first is recommended (see note)
// use the "arktype/config" entrypoint
configure({ numberAllowsNaN: true })
```

```ts title="app.ts"
import "./config.ts"
// import your config file before your arktype!
import { type } from "arktype"
// true
type.number.allows(Number.NaN)

type.number.allows(Number.NaN) // true
```

</td>
</tr>
<tr>
<td>**scope**</td>
<td>all Types parsed in the configured Scope</td>
<td>

```ts
const myScope = scope(
{ user: { age: "number < 100" } },
Expand All @@ -57,12 +63,14 @@ const parsedAfter = myScope.type({
// ArkErrors: age must be at most 100 (was unacceptably large)
parsedAfter({ age: 101 })
```

</td>
</tr>
<tr>
<td>**type**</td>
<td>all Types shallowly referenced by the configured Type</td>
<td>

```ts
// avoid logging "was xxx" for password
const password = type("string >= 8").configure({ actual: () => "" })
Expand All @@ -87,7 +95,7 @@ Some options only apply at specific levels, as reflected in the corresponding in

<Callout
type="warn"
title='Use the `"arktype/config"` entrypoint if for global config!'
title='Use the `"arktype/config"` entrypoint in a separate file for global config!'
>
If you need your config to apply to built-in keywords (important for options
like `jitless`, `numberAllowsNaN`, `dateAllowsInvalid`), you should import and
Expand Down
28 changes: 19 additions & 9 deletions ark/docs/content/docs/expressions/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const sameError = type({ a: "string.numeric.parse" }).or({ b: "string.numeric.pa
```

<details>
<summary>Learn the basic set theory behind this restriction</summary>
<summary>Learn the set theory behind this restriction</summary>

If you're relatively new to set-based types, that error might be daunting, but if you take a second to think through the example, it becomes clear why this isn't allowed. The logic of `bad` is essentially:

Expand All @@ -168,27 +168,37 @@ Add a type-only symbol to an existing type so that the only values that satisfy
<SyntaxTab string>

```ts
// hover to see ArkType's representation of a branded type
const myObject = type({
brandedString: "string#id"
})
// @noErrors
const even = type("(number % 2)#even")
type Even = typeof even.infer

const good: Even = even.assert(2)
// TypeScript: Type 'number' is not assignable to type 'Brand<number, "even">'
const bad: Even = 5
```

</SyntaxTab>

<SyntaxTab fluent>

```ts
// hover to see ArkType's representation of a branded type
const myObject = type({
brandedString: type.string.brand("id")
})
// @noErrors
const even = type.number.divisibleBy(2).brand("even")
type Even = typeof even.infer

const good: Even = even.assert(2)
// TypeScript: Type 'number' is not assignable to type 'Brand<number, "even">'
const bad: Even = 5
```

</SyntaxTab>

</SyntaxTabs>

Brands can be a great way to represent constraints that fall outside the scope TypeScript, but remember they don't change anything about what is enforced at runtime!

For more information on branding in general, check out [this excellent article](https://www.learningtypescript.com/articles/branded-types) from [Josh Goldberg](https://github.com/joshuakgoldberg).

### Narrow

Narrow expressions allow you to add custom validation logic and error messages. You can read more about them in [their intro section](/docs/intro/adding-constraints#narrow).
Expand Down
2 changes: 1 addition & 1 deletion ark/fs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ark/fs",
"version": "0.38.0",
"version": "0.39.0",
"license": "MIT",
"author": {
"name": "David Blass",
Expand Down
20 changes: 2 additions & 18 deletions ark/repo/scratch.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
import { scope, type } from "arktype"
import { type } from "arktype"

const point2d = type({
x: "number",
y: "number",
"+": "delete"
})

const point3d = type({
x: "number",
y: "number",
z: "number",
"+": "delete"
})

const t = point2d.or(point3d)

// number
const n = type("1 | number").expression
const even = type.number.divisibleBy(2).brand("even")
221 changes: 0 additions & 221 deletions ark/repo/scratch/match.ts

This file was deleted.

Loading

0 comments on commit bb5fbf2

Please sign in to comment.