-
Notifications
You must be signed in to change notification settings - Fork 163
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
573cdb1
commit ecc71e8
Showing
396 changed files
with
18,727 additions
and
6,877 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
## [0.32.0](https://www.npmjs.com/package/@sinclair/typebox/v/0.32.0) | ||
|
||
## Overview | ||
|
||
Revision 0.32.0 is milestone revision. This revision carries out a complete modularization of `Type.*` and `Value.*`, as well as remodelling the TypeBox API to support selectively importing individual types. In addition to project restructuring, this revision also includes two new Types (`Const` and `Deref`) two new Value functions (`Clean` and `Default`) as well as many additional optimizations to type inference. | ||
|
||
This is a significant revision for TypeBox. The updates in this revision should not break most users, however, many internal inference types that were previously exported on `typebox.ts` have been made private within module. Testing has been carried out to ensure the documented frontend for TypeBox is retained, however due to internal inference types being made fully private. Some users may experience some breakage. | ||
|
||
A minor revision is required. | ||
|
||
## Contents | ||
|
||
- Enhancements | ||
- [Modularization](#Modularization) | ||
- [Types](#Modularization-Types) | ||
- [Values](#Modularization-Values) | ||
- [Bundle Size](#Modularization-Bundle-Size) | ||
- [Types](#Types) | ||
- [Const](#Const) | ||
- [Deref](#Deref) | ||
- [Values](#Values) | ||
- [Clean](#Clean) | ||
- [Default](#Default) | ||
- Deprecations | ||
- [RegExp](#RegExp) | ||
- [TypeSystem Format](#TypeSystemFormat) | ||
- [TypeSystem Type](#TypeSystemType) | ||
- Breaking | ||
- [JsonTypeBuilder and JavaScriptTypeBuilder](#JsonTypeBuilder-and-JavaScriptTypeBuilder) | ||
- [TypeSystemPolicy](#TypeSystemPolicy) | ||
|
||
## Modularization | ||
|
||
Revision 0.32.0 carries out a complete restructuring of TypeBox's internals to modularize the type system. These updates enable for the following. | ||
|
||
- Enable bundlers to properly tree-shake TypeBox's type system. | ||
- Enable additional options for integrators re-exporting TypeBox's interface. | ||
- Further optimize bundle sizes for Value and TypeCompiler. | ||
- Improve maintainability of the type system. | ||
- Make nessasarily provisions for ESM publishing. | ||
|
||
### Types | ||
|
||
Revision 0.32.0 now exposes `Type.*` as a default export as well as enables all types to be selectively imported. When importing selective types, bundlers may be able to tree-shake unused types. The following imports are now supported. | ||
|
||
```typescript | ||
import { Type, type Static } from '@sinclair/typebox' // classic | ||
import Type, { type Static } from '@sinclair/typebox' // modern | ||
import { Object, String, Number, ... } from '@sinclair/typebox' // selective | ||
``` | ||
### Values | ||
Revision 0.32.0 carries out modularization of `Value.*`. Individual Value functions can not be imported directly without specifying the sub module path. | ||
```typescript | ||
import { Value } from '@sinclair/typebox/value' // classic | ||
import Value from '@sinclair/typebox/value' // modern | ||
import { Check, Clone, Cast } from '@sinclair/typebox/value' // selective | ||
``` | ||
### Bundle Size | ||
The modularization in 0.32.0 has enabled significant bundle size optimizations across Value, TypeCompiler, Errors and System sub modules. | ||
Revision 0.31.0 | ||
``` | ||
┌──────────────────────┬────────────┬────────────┬─────────────┐ | ||
│ (index) │ Compiled │ Minified │ Compression │ | ||
├──────────────────────┼────────────┼────────────┼─────────────┤ | ||
│ typebox/compiler │ '163.6 kb' │ ' 71.6 kb' │ '2.28 x' │ | ||
│ typebox/errors │ '113.3 kb' │ ' 50.1 kb' │ '2.26 x' │ | ||
│ typebox/system │ ' 83.9 kb' │ ' 37.5 kb' │ '2.24 x' │ | ||
│ typebox/value │ '191.1 kb' │ ' 82.3 kb' │ '2.32 x' │ | ||
│ typebox │ ' 73.8 kb' │ ' 32.3 kb' │ '2.29 x' │ | ||
└──────────────────────┴────────────┴────────────┴─────────────┘ | ||
``` | ||
|
||
Revision 0.32.0 | ||
|
||
``` | ||
┌──────────────────────┬────────────┬────────────┬─────────────┐ | ||
│ (index) │ Compiled │ Minified │ Compression │ | ||
├──────────────────────┼────────────┼────────────┼─────────────┤ | ||
│ typebox/compiler │ '109.3 kb' │ ' 48.5 kb' │ '2.25 x' │ | ||
│ typebox/errors │ ' 54.8 kb' │ ' 24.9 kb' │ '2.20 x' │ | ||
│ typebox/system │ ' 12.1 kb' │ ' 6.1 kb' │ '1.98 x' │ | ||
│ typebox/type │ ' 77.8 kb' │ ' 32.8 kb' │ '2.37 x' │ | ||
│ typebox/value │ '143.7 kb' │ ' 61.2 kb' │ '2.35 x' │ | ||
│ typebox │ ' 77.9 kb' │ ' 32.8 kb' │ '2.37 x' │ | ||
└──────────────────────┴────────────┴────────────┴─────────────┘ | ||
``` |
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,148 @@ | ||
/*-------------------------------------------------------------------------- | ||
@sinclair/typebox | ||
The MIT License (MIT) | ||
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
---------------------------------------------------------------------------*/ | ||
|
||
import * as Types from '@sinclair/typebox' | ||
|
||
// ------------------------------------------------------------------- | ||
// Annotation | ||
// | ||
// Generates TypeScript Type Annotations from TypeBox types | ||
// ------------------------------------------------------------------- | ||
/** Generates TypeScript Type Annotations from TypeBox types */ | ||
export namespace Annotation { | ||
// ----------------------------------------------------------------- | ||
// Escape | ||
// ----------------------------------------------------------------- | ||
function Escape(content: string) { | ||
return content.replace(/'/g, "\\'") | ||
} | ||
// ----------------------------------------------------------------- | ||
// Types | ||
// ----------------------------------------------------------------- | ||
function Intersect(schema: Types.TSchema[], references: Types.TSchema[]): string { | ||
const [L, ...R] = schema | ||
// prettier-ignore | ||
return R.length === 0 | ||
? `${Visit(L, references)}` | ||
: `${Visit(L, references)} & ${Intersect(R, references)}` | ||
} | ||
function Union(schema: Types.TSchema[], references: Types.TSchema[]): string { | ||
const [L, ...R] = schema | ||
// prettier-ignore | ||
return R.length === 0 | ||
? `${Visit(L, references)}` | ||
: `${Visit(L, references)} | ${Union(R, references)}` | ||
} | ||
function Tuple(schema: Types.TSchema[], references: Types.TSchema[]): string { | ||
const [L, ...R] = schema | ||
// prettier-ignore | ||
return R.length > 0 | ||
? `${Visit(L, references)}, ${Tuple(R, references)}` | ||
: `` | ||
} | ||
function Property(schema: Types.TProperties, K: string, references: Types.TSchema[]): string { | ||
const TK = schema[K] | ||
// prettier-ignore | ||
return ( | ||
Types.TypeGuard.TOptional(TK) && Types.TypeGuard.TReadonly(TK) ? `readonly ${K}?: ${Visit(TK, references)}` : | ||
Types.TypeGuard.TReadonly(TK) ? `readonly ${K}: ${Visit(TK, references)}` : | ||
Types.TypeGuard.TOptional(TK) ? `${K}?: ${Visit(TK, references)}` : | ||
`${K}: ${Visit(TK, references)}` | ||
) | ||
} | ||
function Properties(schema: Types.TProperties, K: string[], references: Types.TSchema[]): string { | ||
const [L, ...R] = K | ||
// prettier-ignore | ||
return R.length === 0 | ||
? `${Property(schema, L, references)}` | ||
: `${Property(schema, L, references)}; ${Properties(schema, R, references)}` | ||
} | ||
function Parameters(schema: Types.TSchema[], I: number, references: Types.TSchema[]): string { | ||
const [L, ...R] = schema | ||
// prettier-ignore | ||
return R.length === 0 | ||
? `param_${I}: ${Visit(L, references)}` | ||
: `param_${I}: ${Visit(L, references)}, ${Parameters(R, I + 1, references)}` | ||
} | ||
function Literal(schema: Types.TLiteral, references: Types.TSchema[]): string { | ||
return typeof schema.const === 'string' ? `'${Escape(schema.const)}'` : schema.const.toString() | ||
} | ||
function Record(schema: Types.TRecord, references: Types.TSchema[]): string { | ||
// prettier-ignore | ||
return ( | ||
Types.PatternBooleanExact in schema.patternProperties ? `Record<boolean, ${Visit(schema.patternProperties[Types.PatternBooleanExact], references)}>` : | ||
Types.PatternNumberExact in schema.patternProperties ? `Record<number, ${Visit(schema.patternProperties[Types.PatternNumberExact], references)}>` : | ||
Types.PatternStringExact in schema.patternProperties ? `Record<string, ${Visit(schema.patternProperties[Types.PatternStringExact], references)}>` : | ||
`{}` | ||
) | ||
} | ||
function TemplateLiteral(schema: Types.TTemplateLiteral, references: Types.TSchema[]) { | ||
const E = Types.TemplateLiteralParseExact(schema.pattern) | ||
if (!Types.IsTemplateLiteralFinite(E)) return 'string' | ||
return [...Types.TemplateLiteralGenerate(E)].map((literal) => `'${Escape(literal)}'`).join(' | ') | ||
} | ||
function Visit(schema: Types.TSchema, references: Types.TSchema[]): string { | ||
// prettier-ignore | ||
return ( | ||
Types.TypeGuard.TAny(schema) ? 'any' : | ||
Types.TypeGuard.TArray(schema) ? `${Visit(schema.items, references)}[]` : | ||
Types.TypeGuard.TAsyncIterator(schema) ? `AsyncIterableIterator<${Visit(schema.items, references)}>` : | ||
Types.TypeGuard.TBigInt(schema) ? `bigint` : | ||
Types.TypeGuard.TBoolean(schema) ? `boolean` : | ||
Types.TypeGuard.TConstructor(schema) ? `new (${Parameters(schema.parameter, 0, references)}) => ${Visit(schema.returns, references)}` : | ||
Types.TypeGuard.TDate(schema) ? 'Date' : | ||
Types.TypeGuard.TFunction(schema) ? `(${Parameters(schema.parameters, 0, references)}) => ${Visit(schema.returns, references)}` : | ||
Types.TypeGuard.TInteger(schema) ? 'number' : | ||
Types.TypeGuard.TIntersect(schema) ? `(${Intersect(schema.allOf, references)})` : | ||
Types.TypeGuard.TIterator(schema) ? `IterableIterator<${Visit(schema.items, references)}>` : | ||
Types.TypeGuard.TLiteral(schema) ? `${Literal(schema, references)}` : | ||
Types.TypeGuard.TNever(schema) ? `never` : | ||
Types.TypeGuard.TNull(schema) ? `null` : | ||
Types.TypeGuard.TNot(schema) ? 'unknown' : | ||
Types.TypeGuard.TNumber(schema) ? 'number' : | ||
Types.TypeGuard.TObject(schema) ? `{ ${Properties(schema.properties, Object.getOwnPropertyNames(schema.properties), references)} }` : | ||
Types.TypeGuard.TPromise(schema) ? `Promise<${Visit(schema.item, references)}>` : | ||
Types.TypeGuard.TRecord(schema) ? `${Record(schema, references)}` : | ||
Types.TypeGuard.TRef(schema) ? `${Visit(Types.Type.Deref(schema, references), references)}` : | ||
Types.TypeGuard.TString(schema) ? 'string' : | ||
Types.TypeGuard.TSymbol(schema) ? 'symbol' : | ||
Types.TypeGuard.TTemplateLiteral(schema) ? `${TemplateLiteral(schema, references)}` : | ||
Types.TypeGuard.TThis(schema) ? 'unknown' : // requires named interface | ||
Types.TypeGuard.TTuple(schema) ? `[${Tuple(schema.items || [], references)}]` : | ||
Types.TypeGuard.TUint8Array(schema) ? `Uint8Array` : | ||
Types.TypeGuard.TUndefined(schema) ? 'undefined' : | ||
Types.TypeGuard.TUnion(schema) ? `${Union(schema.anyOf, references)}` : | ||
Types.TypeGuard.TVoid(schema) ? `void` : | ||
'unknown' | ||
) | ||
} | ||
/** Generates a TypeScript annotation for the given schema */ | ||
export function Code(schema: Types.TSchema, references: Types.TSchema[] = []): string { | ||
return Visit(schema, references) | ||
} | ||
} |
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 @@ | ||
export * from './annotation' |
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
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.