@badrap/valita
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -248,7 +248,18 @@ /** | ||
get _matcher(): TaggedMatcher; | ||
optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
/** | ||
* Return new optional type that can not be used as a standalone | ||
* validator. Rather, it's meant to be used as a with object validators, | ||
* to mark one of the object's properties as _optional_. Optional property | ||
* types accept both the original type, `undefined` and missing properties. | ||
* | ||
* The optional `defaultFn` function, if provided, will be called each | ||
* time a value that is missing or `undefined` is parsed. | ||
* | ||
* @param [defaultFn] - An optional function returning the default value. | ||
*/ | ||
abstract optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
abstract optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
abstract optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
abstract optional(): Optional<Output>; | ||
/** | ||
* @deprecated Instead of `.default(x)` use `.optional(() => x)`. | ||
@@ -301,4 +312,50 @@ */ | ||
assert<T extends Output>(func: ((v: Output, options: ParseOptions) => v is T) | ((v: Output, options: ParseOptions) => boolean), error?: CustomError): Type<T>; | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom mapping for the source validator's output values. | ||
* | ||
* The mapped value's type doesn't have to stay same, but mapping must | ||
* always succeed (i.e. not throw) for all values that the source validator | ||
* outputs. | ||
* | ||
* @example | ||
* ```ts | ||
* const stringLength = v.string().assert((s) => s.length); | ||
* stringLength.parse("Hello, World!"); | ||
* // 13 | ||
* stringLength.parse(1); | ||
* // ValitaError: invalid_type at . (expected string) | ||
* ``` | ||
* | ||
* @param func - The mapping function. | ||
*/ | ||
map<T extends Literal>(func: (v: Output, options: ParseOptions) => T): Type<T>; | ||
map<T>(func: (v: Output, options: ParseOptions) => T): Type<T>; | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom parsing for the source validator's output values. | ||
* | ||
* Unlike `.map`, `.chain` can also be used for cases where the | ||
* transformation might fail. If the transformation fails, return an error | ||
* with an optional message with `err(...)`. If not, then return the | ||
* transformed value with `ok(...)`. | ||
* | ||
* @example A parser for date strings, returns `Date` objects on success. | ||
* ```ts | ||
* const DateType = v.string().chain((s) => { | ||
* const date = new Date(s); | ||
* if (isNaN(+date)) { | ||
* return v.err("invalid date"); | ||
* } | ||
* return v.ok(date); | ||
* }); | ||
* | ||
* Date.parse("2022-01-01"); | ||
* // 2022-01-01T00:00:00.000Z | ||
* Date.parse("foo"); | ||
* // ValitaError: custom_error at . (invalid date) | ||
* ``` | ||
* | ||
* @param func - The parsing function. | ||
*/ | ||
chain<T extends Literal>(func: (v: Output, options: ParseOptions) => ValitaResult<T>): Type<T>; | ||
@@ -313,2 +370,6 @@ chain<T>(func: (v: Output, options: ParseOptions) => ValitaResult<T>): Type<T>; | ||
abstract name: TypeName; | ||
optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
/** | ||
@@ -336,8 +397,9 @@ * Return new validator that accepts both the original type and `null`. | ||
declare class Optional<Output = unknown> extends AbstractType<Output | undefined> { | ||
/** @internal */ | ||
private readonly _type; | ||
readonly type: Type<Output>; | ||
readonly name = "optional"; | ||
constructor( | ||
/** @internal */ | ||
_type: AbstractType<Output>); | ||
constructor(type: Type<Output>); | ||
optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
_createMatcher(): TaggedMatcher; | ||
@@ -344,0 +406,0 @@ _toTerminals(func: (t: TerminalType) => void): void; |
@@ -353,26 +353,2 @@ "use strict"; | ||
} | ||
/** | ||
* Return new optional type that can not be used as a standalone | ||
* validator. Rather, it's meant to be used as a with object validators, | ||
* to mark one of the object's properties as _optional_. Optional property | ||
* types accept both the original type, `undefined` and missing properties. | ||
* | ||
* The optional `defaultFn` function, if provided, will be called each | ||
* time a value that is missing or `undefined` is parsed. | ||
* | ||
* @param [defaultFn] - An optional function returning the default value. | ||
*/ | ||
optional(defaultFn) { | ||
// If this type is already Optional there's no need to wrap it inside | ||
// a new Optional instance. | ||
const optional = this.name === "optional" | ||
? this | ||
: new Optional(this); | ||
if (!defaultFn) { | ||
return optional; | ||
} | ||
return new TransformType(optional, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
default(defaultValue) { | ||
@@ -430,21 +406,2 @@ const defaultResult = ok(defaultValue); | ||
} | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom mapping for the source validator's output values. | ||
* | ||
* The mapped value's type doesn't have to stay same, but mapping must | ||
* always succeed (i.e. not throw) for all values that the source validator | ||
* outputs. | ||
* | ||
* @example | ||
* ```ts | ||
* const stringLength = v.string().assert((s) => s.length); | ||
* stringLength.parse("Hello, World!"); | ||
* // 13 | ||
* stringLength.parse(1); | ||
* // ValitaError: invalid_type at . (expected string) | ||
* ``` | ||
* | ||
* @param func - The mapping function. | ||
*/ | ||
map(func) { | ||
@@ -456,29 +413,2 @@ return new TransformType(this, (v, options) => ({ | ||
} | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom parsing for the source validator's output values. | ||
* | ||
* Unlike `.map`, `.chain` can also be used for cases where the | ||
* transformation might fail. If the transformation fails, return an error | ||
* with an optional message with `err(...)`. If not, then return the | ||
* transformed value with `ok(...)`. | ||
* | ||
* @example A parser for date strings, returns `Date` objects on success. | ||
* ```ts | ||
* const DateType = v.string().chain((s) => { | ||
* const date = new Date(s); | ||
* if (isNaN(+date)) { | ||
* return v.err("invalid date"); | ||
* } | ||
* return v.ok(date); | ||
* }); | ||
* | ||
* Date.parse("2022-01-01"); | ||
* // 2022-01-01T00:00:00.000Z | ||
* Date.parse("foo"); | ||
* // ValitaError: custom_error at . (invalid date) | ||
* ``` | ||
* | ||
* @param func - The parsing function. | ||
*/ | ||
chain(func) { | ||
@@ -495,2 +425,13 @@ return new TransformType(this, (v, options) => { | ||
class Type extends AbstractType { | ||
optional(defaultFn) { | ||
// If this type is already Optional there's no need to wrap it inside | ||
// a new Optional instance. | ||
const optional = new Optional(this); | ||
if (!defaultFn) { | ||
return optional; | ||
} | ||
return new TransformType(optional, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
/** | ||
@@ -576,11 +517,17 @@ * Return new validator that accepts both the original type and `null`. | ||
class Optional extends AbstractType { | ||
constructor( | ||
/** @internal */ | ||
_type) { | ||
constructor(type) { | ||
super(); | ||
this._type = _type; | ||
this.type = type; | ||
this.name = "optional"; | ||
} | ||
optional(defaultFn) { | ||
if (!defaultFn) { | ||
return this; | ||
} | ||
return new TransformType(this, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
_createMatcher() { | ||
const matcher = this._type._matcher; | ||
const matcher = this.type._matcher; | ||
return taggedMatcher(TAG_OPTIONAL, (v, flags) => v === undefined || flags & FLAG_MISSING_VALUE | ||
@@ -593,3 +540,3 @@ ? undefined | ||
func(undefined_()); | ||
this._type._toTerminals(func); | ||
this.type._toTerminals(func); | ||
} | ||
@@ -596,0 +543,0 @@ } |
@@ -248,7 +248,18 @@ /** | ||
get _matcher(): TaggedMatcher; | ||
optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
/** | ||
* Return new optional type that can not be used as a standalone | ||
* validator. Rather, it's meant to be used as a with object validators, | ||
* to mark one of the object's properties as _optional_. Optional property | ||
* types accept both the original type, `undefined` and missing properties. | ||
* | ||
* The optional `defaultFn` function, if provided, will be called each | ||
* time a value that is missing or `undefined` is parsed. | ||
* | ||
* @param [defaultFn] - An optional function returning the default value. | ||
*/ | ||
abstract optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
abstract optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
abstract optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
abstract optional(): Optional<Output>; | ||
/** | ||
* @deprecated Instead of `.default(x)` use `.optional(() => x)`. | ||
@@ -301,4 +312,50 @@ */ | ||
assert<T extends Output>(func: ((v: Output, options: ParseOptions) => v is T) | ((v: Output, options: ParseOptions) => boolean), error?: CustomError): Type<T>; | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom mapping for the source validator's output values. | ||
* | ||
* The mapped value's type doesn't have to stay same, but mapping must | ||
* always succeed (i.e. not throw) for all values that the source validator | ||
* outputs. | ||
* | ||
* @example | ||
* ```ts | ||
* const stringLength = v.string().assert((s) => s.length); | ||
* stringLength.parse("Hello, World!"); | ||
* // 13 | ||
* stringLength.parse(1); | ||
* // ValitaError: invalid_type at . (expected string) | ||
* ``` | ||
* | ||
* @param func - The mapping function. | ||
*/ | ||
map<T extends Literal>(func: (v: Output, options: ParseOptions) => T): Type<T>; | ||
map<T>(func: (v: Output, options: ParseOptions) => T): Type<T>; | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom parsing for the source validator's output values. | ||
* | ||
* Unlike `.map`, `.chain` can also be used for cases where the | ||
* transformation might fail. If the transformation fails, return an error | ||
* with an optional message with `err(...)`. If not, then return the | ||
* transformed value with `ok(...)`. | ||
* | ||
* @example A parser for date strings, returns `Date` objects on success. | ||
* ```ts | ||
* const DateType = v.string().chain((s) => { | ||
* const date = new Date(s); | ||
* if (isNaN(+date)) { | ||
* return v.err("invalid date"); | ||
* } | ||
* return v.ok(date); | ||
* }); | ||
* | ||
* Date.parse("2022-01-01"); | ||
* // 2022-01-01T00:00:00.000Z | ||
* Date.parse("foo"); | ||
* // ValitaError: custom_error at . (invalid date) | ||
* ``` | ||
* | ||
* @param func - The parsing function. | ||
*/ | ||
chain<T extends Literal>(func: (v: Output, options: ParseOptions) => ValitaResult<T>): Type<T>; | ||
@@ -313,2 +370,6 @@ chain<T>(func: (v: Output, options: ParseOptions) => ValitaResult<T>): Type<T>; | ||
abstract name: TypeName; | ||
optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
/** | ||
@@ -336,8 +397,9 @@ * Return new validator that accepts both the original type and `null`. | ||
declare class Optional<Output = unknown> extends AbstractType<Output | undefined> { | ||
/** @internal */ | ||
private readonly _type; | ||
readonly type: Type<Output>; | ||
readonly name = "optional"; | ||
constructor( | ||
/** @internal */ | ||
_type: AbstractType<Output>); | ||
constructor(type: Type<Output>); | ||
optional<T extends Literal>(defaultFn: <X extends T>() => X): Type<Exclude<Output, undefined> | T>; | ||
optional(defaultFn: () => Exclude<Output, undefined>): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
_createMatcher(): TaggedMatcher; | ||
@@ -344,0 +406,0 @@ _toTerminals(func: (t: TerminalType) => void): void; |
@@ -353,26 +353,2 @@ "use strict"; | ||
} | ||
/** | ||
* Return new optional type that can not be used as a standalone | ||
* validator. Rather, it's meant to be used as a with object validators, | ||
* to mark one of the object's properties as _optional_. Optional property | ||
* types accept both the original type, `undefined` and missing properties. | ||
* | ||
* The optional `defaultFn` function, if provided, will be called each | ||
* time a value that is missing or `undefined` is parsed. | ||
* | ||
* @param [defaultFn] - An optional function returning the default value. | ||
*/ | ||
optional(defaultFn) { | ||
// If this type is already Optional there's no need to wrap it inside | ||
// a new Optional instance. | ||
const optional = this.name === "optional" | ||
? this | ||
: new Optional(this); | ||
if (!defaultFn) { | ||
return optional; | ||
} | ||
return new TransformType(optional, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
default(defaultValue) { | ||
@@ -430,21 +406,2 @@ const defaultResult = ok(defaultValue); | ||
} | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom mapping for the source validator's output values. | ||
* | ||
* The mapped value's type doesn't have to stay same, but mapping must | ||
* always succeed (i.e. not throw) for all values that the source validator | ||
* outputs. | ||
* | ||
* @example | ||
* ```ts | ||
* const stringLength = v.string().assert((s) => s.length); | ||
* stringLength.parse("Hello, World!"); | ||
* // 13 | ||
* stringLength.parse(1); | ||
* // ValitaError: invalid_type at . (expected string) | ||
* ``` | ||
* | ||
* @param func - The mapping function. | ||
*/ | ||
map(func) { | ||
@@ -456,29 +413,2 @@ return new TransformType(this, (v, options) => ({ | ||
} | ||
/** | ||
* Derive a new validator that uses the provided mapping function to | ||
* perform custom parsing for the source validator's output values. | ||
* | ||
* Unlike `.map`, `.chain` can also be used for cases where the | ||
* transformation might fail. If the transformation fails, return an error | ||
* with an optional message with `err(...)`. If not, then return the | ||
* transformed value with `ok(...)`. | ||
* | ||
* @example A parser for date strings, returns `Date` objects on success. | ||
* ```ts | ||
* const DateType = v.string().chain((s) => { | ||
* const date = new Date(s); | ||
* if (isNaN(+date)) { | ||
* return v.err("invalid date"); | ||
* } | ||
* return v.ok(date); | ||
* }); | ||
* | ||
* Date.parse("2022-01-01"); | ||
* // 2022-01-01T00:00:00.000Z | ||
* Date.parse("foo"); | ||
* // ValitaError: custom_error at . (invalid date) | ||
* ``` | ||
* | ||
* @param func - The parsing function. | ||
*/ | ||
chain(func) { | ||
@@ -495,2 +425,13 @@ return new TransformType(this, (v, options) => { | ||
class Type extends AbstractType { | ||
optional(defaultFn) { | ||
// If this type is already Optional there's no need to wrap it inside | ||
// a new Optional instance. | ||
const optional = new Optional(this); | ||
if (!defaultFn) { | ||
return optional; | ||
} | ||
return new TransformType(optional, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
/** | ||
@@ -576,11 +517,17 @@ * Return new validator that accepts both the original type and `null`. | ||
class Optional extends AbstractType { | ||
constructor( | ||
/** @internal */ | ||
_type) { | ||
constructor(type) { | ||
super(); | ||
this._type = _type; | ||
this.type = type; | ||
this.name = "optional"; | ||
} | ||
optional(defaultFn) { | ||
if (!defaultFn) { | ||
return this; | ||
} | ||
return new TransformType(this, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
_createMatcher() { | ||
const matcher = this._type._matcher; | ||
const matcher = this.type._matcher; | ||
return taggedMatcher(TAG_OPTIONAL, (v, flags) => v === undefined || flags & FLAG_MISSING_VALUE | ||
@@ -593,3 +540,3 @@ ? undefined | ||
func(undefined_()); | ||
this._type._toTerminals(func); | ||
this.type._toTerminals(func); | ||
} | ||
@@ -596,0 +543,0 @@ } |
{ | ||
"name": "@badrap/valita", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "A validation & parsing library for TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
117
src/index.ts
@@ -545,2 +545,13 @@ /** | ||
/** | ||
* Return new optional type that can not be used as a standalone | ||
* validator. Rather, it's meant to be used as a with object validators, | ||
* to mark one of the object's properties as _optional_. Optional property | ||
* types accept both the original type, `undefined` and missing properties. | ||
* | ||
* The optional `defaultFn` function, if provided, will be called each | ||
* time a value that is missing or `undefined` is parsed. | ||
* | ||
* @param [defaultFn] - An optional function returning the default value. | ||
*/ | ||
// Use `<X extends T>() => X` instead of `() => T` to make literal | ||
@@ -552,3 +563,3 @@ // inference work when an optionals with defaultFn is used as a | ||
// TypeScript 5.4 onwards. | ||
optional<T extends Literal>( | ||
abstract optional<T extends Literal>( | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters | ||
@@ -560,36 +571,10 @@ defaultFn: <X extends T>() => X, | ||
// `Infer<typeof t>[] | never[]`. | ||
optional( | ||
abstract optional( | ||
defaultFn: () => Exclude<Output, undefined>, | ||
): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
/** | ||
* Return new optional type that can not be used as a standalone | ||
* validator. Rather, it's meant to be used as a with object validators, | ||
* to mark one of the object's properties as _optional_. Optional property | ||
* types accept both the original type, `undefined` and missing properties. | ||
* | ||
* The optional `defaultFn` function, if provided, will be called each | ||
* time a value that is missing or `undefined` is parsed. | ||
* | ||
* @param [defaultFn] - An optional function returning the default value. | ||
*/ | ||
optional<T>( | ||
defaultFn?: () => T, | ||
): Type<Exclude<Output, undefined> | T> | Optional<Output> { | ||
// If this type is already Optional there's no need to wrap it inside | ||
// a new Optional instance. | ||
const optional = | ||
this.name === "optional" | ||
? (this as unknown as Optional<Output>) | ||
: new Optional(this); | ||
abstract optional<T>( | ||
defaultFn: () => T, | ||
): Type<Exclude<Output, undefined> | T>; | ||
abstract optional(): Optional<Output>; | ||
if (!defaultFn) { | ||
return optional; | ||
} | ||
return new TransformType(optional, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
/** | ||
@@ -663,6 +648,2 @@ * @deprecated Instead of `.default(x)` use `.optional(() => x)`. | ||
map<T extends Literal>( | ||
func: (v: Output, options: ParseOptions) => T, | ||
): Type<T>; | ||
map<T>(func: (v: Output, options: ParseOptions) => T): Type<T>; | ||
/** | ||
@@ -687,2 +668,6 @@ * Derive a new validator that uses the provided mapping function to | ||
*/ | ||
map<T extends Literal>( | ||
func: (v: Output, options: ParseOptions) => T, | ||
): Type<T>; | ||
map<T>(func: (v: Output, options: ParseOptions) => T): Type<T>; | ||
map<T>(func: (v: Output, options: ParseOptions) => T): Type<T> { | ||
@@ -695,8 +680,2 @@ return new TransformType(this, (v, options) => ({ | ||
chain<T extends Literal>( | ||
func: (v: Output, options: ParseOptions) => ValitaResult<T>, | ||
): Type<T>; | ||
chain<T>( | ||
func: (v: Output, options: ParseOptions) => ValitaResult<T>, | ||
): Type<T>; | ||
/** | ||
@@ -729,4 +708,10 @@ * Derive a new validator that uses the provided mapping function to | ||
*/ | ||
chain<T extends Literal>( | ||
func: (v: Output, options: ParseOptions) => ValitaResult<T>, | ||
): Type<T>; | ||
chain<T>( | ||
func: (v: Output, options: ParseOptions) => ValitaResult<T>, | ||
): Type<T>; | ||
chain<T>( | ||
func: (v: Output, options: ParseOptions) => ValitaResult<T>, | ||
): Type<T> { | ||
@@ -762,2 +747,23 @@ return new TransformType(this, (v, options) => { | ||
optional<T extends Literal>( | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters | ||
defaultFn: <X extends T>() => X, | ||
): Type<Exclude<Output, undefined> | T>; | ||
optional( | ||
defaultFn: () => Exclude<Output, undefined>, | ||
): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
optional(defaultFn?: () => unknown): unknown { | ||
// If this type is already Optional there's no need to wrap it inside | ||
// a new Optional instance. | ||
const optional = new Optional(this); | ||
if (!defaultFn) { | ||
return optional; | ||
} | ||
return new TransformType(optional, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
/** | ||
@@ -859,11 +865,26 @@ * Return new validator that accepts both the original type and `null`. | ||
constructor( | ||
/** @internal */ | ||
private readonly _type: AbstractType<Output>, | ||
) { | ||
constructor(readonly type: Type<Output>) { | ||
super(); | ||
} | ||
optional<T extends Literal>( | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters | ||
defaultFn: <X extends T>() => X, | ||
): Type<Exclude<Output, undefined> | T>; | ||
optional( | ||
defaultFn: () => Exclude<Output, undefined>, | ||
): Type<Exclude<Output, undefined>>; | ||
optional<T>(defaultFn: () => T): Type<Exclude<Output, undefined> | T>; | ||
optional(): Optional<Output>; | ||
optional(defaultFn?: () => unknown): unknown { | ||
if (!defaultFn) { | ||
return this; | ||
} | ||
return new TransformType(this, (v) => { | ||
return v === undefined ? { ok: true, value: defaultFn() } : undefined; | ||
}); | ||
} | ||
_createMatcher(): TaggedMatcher { | ||
const matcher = this._type._matcher; | ||
const matcher = this.type._matcher; | ||
@@ -880,3 +901,3 @@ return taggedMatcher(TAG_OPTIONAL, (v, flags) => | ||
func(undefined_() as TerminalType); | ||
this._type._toTerminals(func); | ||
this.type._toTerminals(func); | ||
} | ||
@@ -883,0 +904,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
500716
8450