You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: ark/docs/content/docs/configuration/index.mdx
+66
Original file line number
Diff line number
Diff line change
@@ -367,6 +367,72 @@ const out = userForm({
367
367
})
368
368
```
369
369
370
+
### exactOptionalPropertyTypes
371
+
372
+
By default, ArkType validates optional keys as if [TypeScript's `exactOptionalPropertyTypes` is set to `true`](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes).
373
+
374
+
<details>
375
+
<summary>See an example</summary>
376
+
377
+
```ts
378
+
const myObj =type({
379
+
"key?": "number"
380
+
})
381
+
382
+
// valid data
383
+
const validResult =myObj({})
384
+
385
+
// Error: key must be a number (was undefined)
386
+
const errorResult =myObj({ key: undefined })
387
+
```
388
+
389
+
</details>
390
+
391
+
This approach allows the most granular control over optionality, as `| undefined` can be added to properties that should accept it.
392
+
393
+
However, if you have not enabled TypeScript's `exactOptionalPropertyTypes` setting, you may globally configure ArkType's `exactOptionalPropertyTypes` to `false` to match TypeScript's behavior. If you do this, we'd recommend making a plan to enable `exactOptionalPropertyTypes` in the future.
394
+
395
+
```ts title="config.ts"
396
+
import { configure } from"arktype/config"
397
+
398
+
// since the default in ArkType is `true`, this will only have an effect if set to `false`
399
+
configure({ exactOptionalPropertyTypes: false })
400
+
```
401
+
402
+
```ts title="app.ts"
403
+
import"./config.ts"
404
+
// import your config file before arktype
405
+
import { type } from"arktype"
406
+
407
+
const myObj =type({
408
+
"key?": "number"
409
+
})
410
+
411
+
// valid data
412
+
const validResult =myObj({})
413
+
414
+
// now also valid data (would be an error by default)
415
+
const secondResult =myObj({ key: undefined })
416
+
```
417
+
418
+
<Callouttype="warn"title="exactOptionalPropertyTypes does not yet affect default values!">
419
+
420
+
```ts
421
+
const myObj =type({
422
+
key: "number = 5"
423
+
})
424
+
425
+
// { key: 5 }
426
+
const omittedResult =myObj({})
427
+
428
+
// { key: undefined }
429
+
const undefinedResult =myObj({ key: undefined })
430
+
```
431
+
432
+
Support for this is tracked as part of [this broader configurable defaultability issue](https://github.com/arktypeio/arktype/issues/1390).
433
+
434
+
</Callout>
435
+
370
436
### jitless
371
437
372
438
By default, when a `Type` is instantiated, ArkType will precompile optimized validation logic that will run when the type is invoked. This behavior is disabled by default in environments that don't support `new Function`, e.g. Cloudflare Workers.
Copy file name to clipboardexpand all lines: ark/docs/content/docs/objects/index.mdx
+1-1
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ const myObject = type({
91
91
92
92
In TypeScript, there is a setting called [`exactOptionalPropertyTypes`](https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes) that can be set to `true` to enforce the distinction between properties that are missing and properties that are present with the value `undefined`.
93
93
94
-
ArkType mirrors this behavior by default, so if you want to allow `undefined`, you'll need to add it to your value's definition. If you're interested in a builtin configuration option for this setting, we'd love feedback or contributions on [this issue](https://github.com/arktypeio/arktype/issues/1191).
94
+
ArkType mirrors this behavior by default, so if you want to allow `undefined`, you'll need to add it to your value's definition. Though not recommended as a long-term solution, you may also [globally configure `exactOptionalPropertyTypes`](/docs/configuration#exactoptionalpropertytypes) to `false`.
Copy file name to clipboardexpand all lines: ark/type/CHANGELOG.md
+61
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,66 @@
1
1
# arktype
2
2
3
+
## 2.1.12
4
+
5
+
### `exactOptionalPropertyTypes`
6
+
7
+
By default, ArkType validates optional keys as if [TypeScript's `exactOptionalPropertyTypes` is set to `true`](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes).
8
+
9
+
```ts
10
+
const myObj =type({
11
+
"key?": "number"
12
+
})
13
+
14
+
// valid data
15
+
const validResult =myObj({})
16
+
17
+
// Error: key must be a number (was undefined)
18
+
const errorResult =myObj({ key: undefined })
19
+
```
20
+
21
+
This approach allows the most granular control over optionality, as `| undefined` can be added to properties that should accept it.
22
+
23
+
However, if you have not enabled TypeScript's `exactOptionalPropertyTypes` setting, you may globally configure ArkType's `exactOptionalPropertyTypes` to `false` to match TypeScript's behavior. If you do this, we'd recommend making a plan to enable `exactOptionalPropertyTypes` in the future.
24
+
25
+
```ts title="config.ts"
26
+
import { configure } from"arktype/config"
27
+
28
+
// since the default in ArkType is `true`, this will only have an effect if set to `false`
29
+
configure({ exactOptionalPropertyTypes: false })
30
+
```
31
+
32
+
```ts title="app.ts"
33
+
import"./config.ts"
34
+
// import your config file before arktype
35
+
import { type } from"arktype"
36
+
37
+
const myObj =type({
38
+
"key?": "number"
39
+
})
40
+
41
+
// valid data
42
+
const validResult =myObj({})
43
+
44
+
// now also valid data (would be an error by default)
45
+
const secondResult =myObj({ key: undefined })
46
+
```
47
+
48
+
**WARNING: exactOptionalPropertyTypes does not yet affect default values!**
49
+
50
+
```ts
51
+
const myObj =type({
52
+
key: "number = 5"
53
+
})
54
+
55
+
// { key: 5 }
56
+
const omittedResult =myObj({})
57
+
58
+
// { key: undefined }
59
+
const undefinedResult =myObj({ key: undefined })
60
+
```
61
+
62
+
Support for this is tracked as part of [this broader configurable defaultability issue](https://github.com/arktypeio/arktype/issues/1390).
63
+
3
64
## 2.1.11
4
65
5
66
- Expose `select` method directly on `Type` (previously was only available on `.internal`)
0 commit comments