Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add runtime type-checker for bigint #366

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Change Log

- Added runtime type-checker for BigInt.

## 0.67.0

- Big speedup in certain cases (when many mutations are done between `getSnapshot` calls) by delaying the immutable object creation to the point when `getSnapshot` is called.
Expand Down
27 changes: 27 additions & 0 deletions packages/lib/src/types/primitiveBased/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ registerStandardTypeResolver((v) => (v === Number ? typesNumber : undefined))
*/
export class NumberTypeInfo extends TypeInfo {}

/**
* A type that represents any BigInt value.
*
* ```ts
* types.bigint
* ```
*/
export const typesBigInt: IdentityType<bigint> = new TypeChecker(
TypeCheckerBaseType.Primitive,

(value, path) => (typeof value === "bigint" ? null : new TypeCheckError(path, "bigint", value)),

() => "bigint",
(t) => new BigIntTypeInfo(t),

(value) => (typeof value === "bigint" ? (typesBigInt as any) : null),
identityFn,
identityFn
) as any

registerStandardTypeResolver((v) => (v === BigInt ? typesBigInt : undefined))

/**
* `types.bigint` type info.
*/
export class BigIntTypeInfo extends TypeInfo {}

/**
* A type that represents any string value.
*
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/types/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type AnyNonValueType =
| ModelClass<AnyModel>
| StringConstructor
| NumberConstructor
| BigIntConstructor
| BooleanConstructor
| AnyStandardType

Expand All @@ -94,6 +95,9 @@ export type TypeToData<S> = S extends ObjectTypeFunction
: // Number
S extends NumberConstructor
? number
: // BigInt
S extends BigIntConstructor
? bigint
: // Boolean
S extends BooleanConstructor
? boolean
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import { RecordTypeInfo, typesRecord } from "./objectBased/record"
import { RefTypeInfo, typesRef } from "./objectBased/ref"
import { typesEnum } from "./primitiveBased/enum"
import {
BigIntTypeInfo,
BooleanTypeInfo,
LiteralTypeInfo,
NumberTypeInfo,
StringTypeInfo,
typesBigInt,
typesBoolean,
typesLiteral,
typesNull,
Expand All @@ -36,6 +38,7 @@ export { getTypeInfo } from "./getTypeInfo"
export { TypeInfo } from "./TypeChecker"
export type { ObjectTypeInfoProps, ModelTypeInfoProps }
export {
BigIntTypeInfo,
BooleanTypeInfo,
LiteralTypeInfo,
NumberTypeInfo,
Expand All @@ -60,6 +63,7 @@ export const types = {
null: typesNull,
boolean: typesBoolean,
number: typesNumber,
bigint: typesBigInt,
string: typesString,
or: typesOr,
maybe: typesMaybe,
Expand Down
21 changes: 21 additions & 0 deletions packages/lib/test/types/typeChecking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
arraySet,
ArraySetTypeInfo,
ArrayTypeInfo,
BigIntTypeInfo,
BooleanTypeInfo,
customRef,
Frozen,
Expand Down Expand Up @@ -222,6 +223,26 @@ test("simple number", () => {
expectValidTypeInfo(type, NumberTypeInfo)
})

test("bigint", () => {
const type = types.bigint
assert(_ as TypeToData<typeof type>, _ as bigint)

expectTypeCheckOk(type, 6n)
expectTypeCheckFail(type, "ho", [], "bigint")

expectValidTypeInfo(type, BigIntTypeInfo)
})

test("simple bigint", () => {
const type = BigInt
assert(_ as TypeToData<typeof type>, _ as bigint)

expectTypeCheckOk(type, 6n)
expectTypeCheckFail(type, "ho", [], "bigint")

expectValidTypeInfo(type, BigIntTypeInfo)
})

test("string", () => {
const type = types.string
assert(_ as TypeToData<typeof type>, _ as string)
Expand Down
4 changes: 4 additions & 0 deletions packages/site/docs/runtimeTypeChecking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ A type that represents any number value.

A type that represents any integer number value.

### `types.bigint` / `BigInt`

A type that represents any BigInt value.

### `types.string` / `String`

A type that represents any string value.
Expand Down