Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.Contents
Type.Not Representation Change
The
Type.Not
was first introduced in Revision 0.26.0. This type accepted two arguments, the first is thenot
type, the second is theallowed
type. In 0.26.0, TypeBox would treat theallowed
type as the inferred type with the schema represented in the following form.0.26.0
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.0.29.0
Revision 0.29.0 changes the
Type.Not
type to take a singlenot
argument only. This type statically infers asunknown
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 theallOf
representation. The type inference works in this case as intersectednumber & unknown
yields the most narrowed type (which isnumber
)The 0.29.0
Not
type properly represents the JSON Schemanot
keyword in its simplest form, as well as making better use of intersection type narrowing capabilities of TypeScript.Not Inversion
The not type can be inverted through nesting.
Inference Limitations
Not types are synonymous with the concept of negated types which are not supported in the TypeScript language. Because of this, it is not currently possible to infer negated types in a way one would naturally expect for some cases. Consider the following.
As such, the use of Not types should be used with some consideration to current limitations, and reserved primarily for narrowing cases such as the following.