-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b6c984
commit 7e2d660
Showing
22 changed files
with
427 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
## [0.29.0](https://www.npmjs.com/package/@sinclair/typebox/v/0.29.0) | ||
|
||
## Overview | ||
|
||
Revision 0.29.0 makes a minor interface and schema representation change to the `Type.Not` type. This revision also includes a fix for indexed access types on TypeScript 5.1.6. | ||
|
||
As this revision constitutes a breaking representation change for `Type.Not`, a minor semver revision is required. | ||
|
||
## Type.Not Representation Change | ||
|
||
The `Type.Not` was first introduced in Revision 0.26.0. This type accepted two arguments, the first is the `not` type, the second is the `allowed` type. In 0.26.0, TypeBox would treat the `allowed` type as the inferred type with the schema represented in the following form. | ||
|
||
### 0.26.0 | ||
|
||
```typescript | ||
// allow all numbers except the number 42 | ||
// | ||
const T = Type.Not(Type.Literal(42), Type.Number()) | ||
// ^ ^ | ||
// not type allowed type | ||
|
||
// represented as | ||
// | ||
const T = { | ||
allOf: [ | ||
{ not: { const: 42 } }, | ||
{ type: 'number' } | ||
] | ||
} | ||
|
||
// inferred as | ||
// | ||
type T = Static<typeof T> // type T = number | ||
``` | ||
In 0.26.0. the rationale for the second `allowed` argument was provide a correct static type to infer, where one could describe what the type wasn't on the first and what it was on the second (with inference of operating on the second argument). This approach was to echo possible suggestions for negated type syntax in TypeScript. | ||
```typescript | ||
type T = number & not 42 // not actual typescript syntax! | ||
``` | ||
### 0.29.0 | ||
Revision 0.29.0 changes the `Type.Not` type to take a single `not` argument only. This type statically infers as `unknown` | ||
```typescript | ||
// allow all types except the literal number 42 | ||
// | ||
const T = Type.Not(Type.Literal(42)) | ||
// ^ | ||
// not type | ||
|
||
// represented as | ||
// | ||
const T = { not: { const: 42 } } | ||
|
||
// inferred as | ||
// | ||
type T = Static<typeof T> // type T = unknown | ||
|
||
``` | ||
### Upgrading to 0.29.0 | ||
|
||
In revision 0.29.0, you can express the 0.26.0 Not type via `Type.Intersect` which explicitly creates the `allOf` representation. The type inference works in this case as intersected `number & unknown` yields the most narrowed type (which is `number`) | ||
|
||
```typescript | ||
// allow all numbers except the number 42 | ||
// | ||
const T = Type.Intersect([ Type.Not(Type.Literal(42)), Type.Number() ]) | ||
// ^ ^ | ||
// not type allowed type | ||
|
||
// represented as | ||
// | ||
const T = { | ||
allOf: [ | ||
{ not: { const: 42 } }, | ||
{ type: 'number' } | ||
] | ||
} | ||
// inferred as | ||
// | ||
type T = Static<typeof T> // type T = number | ||
``` | ||
The 0.29.0 `Not` type properly represents the JSON Schema `not` keyword in its simplest form, as well as making better use of the type intersection narrowing capabilities of TypeScript with respect to inference. | ||
### Invert Not | ||
In revision 0.29.0, it is possible to invert the `Not` type. TypeBox will track each inversion and statically infer appropriately. | ||
```typescript | ||
// not not string | ||
// | ||
const T = Type.Not(Type.Not(Type.String())) | ||
|
||
// represented as | ||
// | ||
const T = { | ||
not: { | ||
not: { | ||
type: "string" | ||
} | ||
} | ||
} | ||
|
||
// inferred as | ||
// | ||
type T = Static<typeof T> // type T = string | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.