From a670487154724c6e08517a9a1d8bb0d3ede10a3d Mon Sep 17 00:00:00 2001 From: sinclairzx81 Date: Sat, 27 Apr 2024 16:15:28 +0900 Subject: [PATCH] Revision 0.32.26 (#851) * Use Optimized Number Check * Fix Readme Error | Update Overview * Version --- package-lock.json | 4 ++-- package.json | 2 +- readme.md | 8 ++++---- src/compiler/compiler.ts | 4 ++-- src/system/policy.ts | 3 +-- src/type/guard/type.ts | 2 +- src/value/guard/guard.ts | 2 +- 7 files changed, 12 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index f8c489ae..a0d06c80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typebox", - "version": "0.32.25", + "version": "0.32.26", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typebox", - "version": "0.32.25", + "version": "0.32.26", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.13.2", diff --git a/package.json b/package.json index 300281f8..f2162e4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typebox", - "version": "0.32.25", + "version": "0.32.26", "description": "Json Schema Type Builder with Static Type Resolution for TypeScript", "keywords": [ "typescript", diff --git a/readme.md b/readme.md index 86aac555..239e894e 100644 --- a/readme.md +++ b/readme.md @@ -53,7 +53,7 @@ type T = Static // type T = { TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation. -This library is designed to allow Json Schema to compose with the same flexibility as TypeScript's programmable type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire. +This library is designed to allow Json Schema to compose with a similar flexibility to TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire. License MIT @@ -895,9 +895,9 @@ const K = Type.TemplateLiteral('prop${A|B|C}') // const K: TTemplateLitera // ]> const R = Type.Record(K, Type.String()) // const R: TObject<{ - // hello1: TString, - // hello2: TString, - // hello3: TString, + // propA: TString, + // propB: TString, + // propC: TString, // }> ``` diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 39cb2f4c..8e939f1f 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -210,7 +210,7 @@ export namespace Policy { : `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))` } export function IsNumberLike(value: string): string { - return !TypeSystemPolicy.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'` + return TypeSystemPolicy.AllowNaN ? `typeof ${value} === 'number'` : `Number.isFinite(${value})` } export function IsVoidLike(value: string): string { return TypeSystemPolicy.AllowNullVoid ? `(${value} === undefined || ${value} === null)` : `${value} === undefined` @@ -288,7 +288,7 @@ export namespace TypeCompiler { yield `(typeof ${value} === 'function')` } function* FromInteger(schema: TInteger, references: TSchema[], value: string): IterableIterator { - yield `(typeof ${value} === 'number' && Number.isInteger(${value}))` + yield `Number.isInteger(${value})` if (IsNumber(schema.exclusiveMaximum)) yield `${value} < ${schema.exclusiveMaximum}` if (IsNumber(schema.exclusiveMinimum)) yield `${value} > ${schema.exclusiveMinimum}` if (IsNumber(schema.maximum)) yield `${value} <= ${schema.maximum}` diff --git a/src/system/policy.ts b/src/system/policy.ts index e2d1a08e..400c74b1 100644 --- a/src/system/policy.ts +++ b/src/system/policy.ts @@ -56,8 +56,7 @@ export namespace TypeSystemPolicy { } /** Asserts this value using the AllowNaN policy */ export function IsNumberLike(value: unknown): value is number { - const isNumber = IsNumber(value) - return AllowNaN ? isNumber : isNumber && Number.isFinite(value) + return AllowNaN ? IsNumber(value) : Number.isFinite(value) } /** Asserts this value using the AllowVoidNull policy */ export function IsVoidLike(value: unknown): value is void { diff --git a/src/type/guard/type.ts b/src/type/guard/type.ts index f80b24fe..b2e9daa9 100644 --- a/src/type/guard/type.ts +++ b/src/type/guard/type.ts @@ -30,7 +30,7 @@ import * as ValueGuard from './value' import { Kind, Hint, TransformKind, ReadonlyKind, OptionalKind } from '../symbols/index' import { TypeBoxError } from '../error/index' import { TransformOptions } from '../transform/index' -import { TTemplateLiteral, TTemplateLiteralKind } from '../template-literal/index' +import { TTemplateLiteral } from '../template-literal/index' import { TArray } from '../array/index' import { TBoolean } from '../boolean/index' import type { TRecord } from '../record/index' diff --git a/src/value/guard/guard.ts b/src/value/guard/guard.ts index 8e45b1bd..14c575dc 100644 --- a/src/value/guard/guard.ts +++ b/src/value/guard/guard.ts @@ -171,7 +171,7 @@ export function IsNumber(value: unknown): value is number { } /** Returns true if this value is an integer */ export function IsInteger(value: unknown): value is number { - return IsNumber(value) && Number.isInteger(value) + return Number.isInteger(value) } /** Returns true if this value is bigint */ export function IsBigInt(value: unknown): value is bigint {