Skip to content

Commit

Permalink
refactor(validator): abstract Primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW committed Oct 24, 2024
1 parent a6219b8 commit 4cde539
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// dprint-ignore-file
export type WithThis<T> = T & ThisType<T>

export type AtLeastOneProperty<T> = keyof T extends infer Keys ? Keys extends (
Expand Down Expand Up @@ -196,7 +197,7 @@ export type Replace<

type Cast<A, B> = A extends B ? A : B

type Primitive = string | number | boolean | bigint | symbol | undefined | null
export type Primitive = string | number | boolean | bigint | symbol | undefined | null

export type Narrow<T> = Cast<T, unknown[] | [] | (T extends Primitive ? T : never) | ({
[K in keyof T]: K extends typeof Symbol.species
Expand Down
5 changes: 2 additions & 3 deletions packages/validator/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Primitive } from '@typp/core/types'
import type { Transform } from '@typp/validator/extends'

export function parseBigInt(inputStr: string): bigint {
Expand Down Expand Up @@ -94,8 +95,6 @@ function getMethod(obj: unknown, key: string | symbol): Function | undefined {
throw new TypeError(`${func} is not a function`)
}

type Primitive = number | bigint | boolean | string | symbol | object | null | undefined

/**
* @see https://tc39.es/ecma262/multipage/abstract-operations.html#sec-ordinarytoprimitive
*/
Expand All @@ -111,7 +110,7 @@ function ordinaryToPrimitive(o: object, hint: 'number' | 'string' | (string & {}
if (typeof method === 'function') {
const result = Reflect.apply(method, o, [])
if (typeof result !== 'object') {
return result
return result as Primitive
}
}
}
Expand Down

0 comments on commit 4cde539

Please sign in to comment.