inferred-types 0.32.1 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +17 -5
- package/dist/index.js +9 -4
- package/dist/index.mjs +8 -4
- package/package.json +1 -1
- package/src/types/TypeInfo/IsUndefined.ts +3 -1
- package/src/utility/runtime/conditions/isNull.ts +5 -1
- package/src/utility/runtime/conditions/isString.ts +7 -1
- package/src/utility/runtime/conditions/isUndefined.ts +23 -1
- package/tests/runtime/if-is.spec.ts +14 -1
package/dist/index.d.ts
CHANGED
|
@@ -365,7 +365,7 @@ declare type SimplifyObject<T extends {}> = ExpandRecursively<UnionToIntersectio
|
|
|
365
365
|
*
|
|
366
366
|
* Boolean type utility returns `true` if `T` is undefined; `false` otherwise
|
|
367
367
|
*/
|
|
368
|
-
declare type IsUndefined<T> = T extends undefined ? true : false;
|
|
368
|
+
declare type IsUndefined<T extends Narrowable> = T extends undefined ? true : false;
|
|
369
369
|
/**
|
|
370
370
|
* **IfUndefined**
|
|
371
371
|
*
|
|
@@ -638,7 +638,7 @@ declare function isNull<T extends Narrowable>(i: T): T extends null ? true : fal
|
|
|
638
638
|
* @param ifVal the value (strongly typed) returned if val is `null`
|
|
639
639
|
* @param elseVal the value (strongly typed) returned if val is NOT `null`
|
|
640
640
|
*/
|
|
641
|
-
declare function ifNull<T extends Narrowable, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsNull<T> extends true ? IF : ELSE;
|
|
641
|
+
declare function ifNull<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(val: T, ifVal: IF, elseVal: ELSE): IsNull<T> extends true ? IF : ELSE;
|
|
642
642
|
|
|
643
643
|
declare type IsNumber<T> = T extends number ? true : false;
|
|
644
644
|
declare function isNumber<T>(i: T): T extends number ? true : false;
|
|
@@ -677,7 +677,7 @@ declare function isString<T>(i: T): IsString<T>;
|
|
|
677
677
|
* @param ifVal the value (strongly typed) returned if val is _string_
|
|
678
678
|
* @param elseVal the value (strongly typed) returned if val is NOT a _string
|
|
679
679
|
*/
|
|
680
|
-
declare function ifString<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsString<T> extends true ? IF : ELSE;
|
|
680
|
+
declare function ifString<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(val: T, ifVal: IF, elseVal: ELSE): IsString<T> extends true ? IF : ELSE;
|
|
681
681
|
|
|
682
682
|
declare function isSymbol<T>(i: T): T extends symbol ? true : false;
|
|
683
683
|
|
|
@@ -712,7 +712,19 @@ declare function isTrue<T extends Narrowable>(i: T): IsTrue<T>;
|
|
|
712
712
|
*/
|
|
713
713
|
declare function ifTrue<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsTrue<T> extends true ? IF : ELSE;
|
|
714
714
|
|
|
715
|
-
declare function isUndefined<T>(i: T): undefined extends T ? true : false;
|
|
715
|
+
declare function isUndefined<T extends Narrowable>(i: T): undefined extends T ? true : false;
|
|
716
|
+
/**
|
|
717
|
+
* **ifUndefined**
|
|
718
|
+
*
|
|
719
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
720
|
+
* _undefined_ and returns one of two values (strongly typed) based on the evaluation
|
|
721
|
+
* of this criteria.
|
|
722
|
+
*
|
|
723
|
+
* @param val the value being tested
|
|
724
|
+
* @param ifVal the value (strongly typed) returned if val is `undefined`
|
|
725
|
+
* @param elseVal the value (strongly typed) returned if val is NOT `undefined`
|
|
726
|
+
*/
|
|
727
|
+
declare function ifUndefined<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(val: T, ifVal: IF, elseVal: ELSE): IsUndefined<T> extends true ? IF : ELSE;
|
|
716
728
|
|
|
717
729
|
declare type Type<T extends any, V extends Function> = {
|
|
718
730
|
name: string;
|
|
@@ -2371,4 +2383,4 @@ interface IFluentConfigurator<C> {
|
|
|
2371
2383
|
*/
|
|
2372
2384
|
declare function FluentConfigurator<I>(initial?: I): IFluentConfigurator<{}>;
|
|
2373
2385
|
|
|
2374
|
-
export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AsArray, AsFinalizedConfig, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, Cardinality, Cardinality0, Cardinality1, CardinalityExplicit, CardinalityFilter0, CardinalityFilter1, CardinalityIn, CardinalityInput, CardinalityNode, CardinalityOut, CardinalityTuple, ClosingBracket, Condition, ConditionFilter, Configurator, ConfiguredMap, Constructor, DEFAULT_MANY_TO_ONE_MAPPING, DEFAULT_ONE_TO_MANY_MAPPING, DEFAULT_ONE_TO_ONE_MAPPING, DashToSnake, DashUppercase, Dasherize, DecomposeMapConfig, DefaultManyToOneMapping, DefaultOneToManyMapping, DefaultOneToOneMapping, DefinePropertiesApi, DictArrApi, DictArray, DictArrayFilterCallback, DictArrayKv, DictChangeValue, DictFromKv, DictKvTuple, DictPartialApplication, DictPrependWithFn, DictReturnValues, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, Extends, ExtendsClause, ExtendsNarrowlyClause, FilterContains, FilterDefn, FilterEnds, FilterEquals, FilterFn, FilterGreaterThan, FilterIs, FilterLessThan, FilterNotEqual, FilterStarts, FinalReturn, FinalizedMapConfig, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfBooleanLiteral, IfExtends, IfExtendsThen, IfLiteral, IfNumericLiteral, IfObject, IfOptionalLiteral, IfScalar, IfStringLiteral, Include, Includes, IntersectingKeys, IsArray, IsBoolean, IsBooleanLiteral, IsCapitalized, IsFalse, IsFunction, IsLiteral, IsNull, IsNumber, IsNumericLiteral, IsObject, IsOptionalLiteral, IsScalar, IsString, IsStringLiteral, IsTrue, KebabCase, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LogicFunction, LogicalCombinator, LowerAllCaps, LowerAlpha, ManyToMany, ManyToOne, ManyToZero, MapCard, MapCardinality, MapCardinalityFrom, MapCardinalityIllustrated, MapConfig, MapFn, MapFnInput, MapFnOutput, MapIR, MapInput, MapInputFrom, MapOR, MapOutput, MapOutputFrom, MapTo, Mapper, MapperApi, MaybeFalse, MaybeTrue, Model, Mutable, MutableProps, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, NotFilter, Numeric, NumericFilter, NumericKeys, NumericString, ObjectType, OneToMany, OneToOne, OneToZero, Opaque, OpeningBracket, OptRequired, OptionalKeys, OptionalProps, Parenthesis, PascalCase, Pluralize, PrivateKey, PrivateKeys, PublicKeys, Punctuation, PureFluentApi, Replace, RequireProps, RequiredKeys, RequiredProps, Retain, RightWhitespace, RuleDefinition, RuntimeProp, RuntimeType, SameKeys, SecondOfEach, SimplifyObject, SnakeCase, SpecialCharacters, StringDelimiter, StringFilter, StringKeys, StringLength, ToFluent, Transformer, Trim, TrimLeft, TrimRight, TupleToUnion, Type, TypeApi, TypeCondition, TypeDefault, TypeDefinition, TypeGuard, TypeOptions, UndefinedArrayIsUnknown, UndefinedTreatment, UndefinedValue, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, Uniqueness, UnwrapNot, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, Widen, WithNumericKeys, WithStringKeys, WithValue, ZeroToMany, ZeroToOne, ZeroToZero, ZipCode, and, api, arrayToKeyLookup, arrayToObject, asArray, condition, createFnWithProps, createMutationFunction, defineProperties, defineType, dictArr, dictToKv, dictionaryTransform, entries, filter, filterDictArray, fnWithProps, groupBy, idLiteral, idTypeGuard, identity, ifBoolean, ifNull, ifNumber, ifString, ifTrue, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isNotFilter, isNull, isNumber, isNumericFilter, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, literal, mapTo, mapToDict, mapToFn, mapValues, nameLiteral, not, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, withValue };
|
|
2386
|
+
export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AsArray, AsFinalizedConfig, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, Cardinality, Cardinality0, Cardinality1, CardinalityExplicit, CardinalityFilter0, CardinalityFilter1, CardinalityIn, CardinalityInput, CardinalityNode, CardinalityOut, CardinalityTuple, ClosingBracket, Condition, ConditionFilter, Configurator, ConfiguredMap, Constructor, DEFAULT_MANY_TO_ONE_MAPPING, DEFAULT_ONE_TO_MANY_MAPPING, DEFAULT_ONE_TO_ONE_MAPPING, DashToSnake, DashUppercase, Dasherize, DecomposeMapConfig, DefaultManyToOneMapping, DefaultOneToManyMapping, DefaultOneToOneMapping, DefinePropertiesApi, DictArrApi, DictArray, DictArrayFilterCallback, DictArrayKv, DictChangeValue, DictFromKv, DictKvTuple, DictPartialApplication, DictPrependWithFn, DictReturnValues, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, Extends, ExtendsClause, ExtendsNarrowlyClause, FilterContains, FilterDefn, FilterEnds, FilterEquals, FilterFn, FilterGreaterThan, FilterIs, FilterLessThan, FilterNotEqual, FilterStarts, FinalReturn, FinalizedMapConfig, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfBooleanLiteral, IfExtends, IfExtendsThen, IfLiteral, IfNumericLiteral, IfObject, IfOptionalLiteral, IfScalar, IfStringLiteral, Include, Includes, IntersectingKeys, IsArray, IsBoolean, IsBooleanLiteral, IsCapitalized, IsFalse, IsFunction, IsLiteral, IsNull, IsNumber, IsNumericLiteral, IsObject, IsOptionalLiteral, IsScalar, IsString, IsStringLiteral, IsTrue, KebabCase, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LogicFunction, LogicalCombinator, LowerAllCaps, LowerAlpha, ManyToMany, ManyToOne, ManyToZero, MapCard, MapCardinality, MapCardinalityFrom, MapCardinalityIllustrated, MapConfig, MapFn, MapFnInput, MapFnOutput, MapIR, MapInput, MapInputFrom, MapOR, MapOutput, MapOutputFrom, MapTo, Mapper, MapperApi, MaybeFalse, MaybeTrue, Model, Mutable, MutableProps, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, NotFilter, Numeric, NumericFilter, NumericKeys, NumericString, ObjectType, OneToMany, OneToOne, OneToZero, Opaque, OpeningBracket, OptRequired, OptionalKeys, OptionalProps, Parenthesis, PascalCase, Pluralize, PrivateKey, PrivateKeys, PublicKeys, Punctuation, PureFluentApi, Replace, RequireProps, RequiredKeys, RequiredProps, Retain, RightWhitespace, RuleDefinition, RuntimeProp, RuntimeType, SameKeys, SecondOfEach, SimplifyObject, SnakeCase, SpecialCharacters, StringDelimiter, StringFilter, StringKeys, StringLength, ToFluent, Transformer, Trim, TrimLeft, TrimRight, TupleToUnion, Type, TypeApi, TypeCondition, TypeDefault, TypeDefinition, TypeGuard, TypeOptions, UndefinedArrayIsUnknown, UndefinedTreatment, UndefinedValue, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, Uniqueness, UnwrapNot, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, Widen, WithNumericKeys, WithStringKeys, WithValue, ZeroToMany, ZeroToOne, ZeroToZero, ZipCode, and, api, arrayToKeyLookup, arrayToObject, asArray, condition, createFnWithProps, createMutationFunction, defineProperties, defineType, dictArr, dictToKv, dictionaryTransform, entries, filter, filterDictArray, fnWithProps, groupBy, idLiteral, idTypeGuard, identity, ifBoolean, ifNull, ifNumber, ifString, ifTrue, ifTypeOf, ifUndefined, isArray, isBoolean, isFalse, isFunction, isNotFilter, isNull, isNumber, isNumericFilter, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, literal, mapTo, mapToDict, mapToFn, mapValues, nameLiteral, not, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, withValue };
|
package/dist/index.js
CHANGED
|
@@ -56,6 +56,7 @@ __export(src_exports, {
|
|
|
56
56
|
ifString: () => ifString,
|
|
57
57
|
ifTrue: () => ifTrue,
|
|
58
58
|
ifTypeOf: () => ifTypeOf,
|
|
59
|
+
ifUndefined: () => ifUndefined,
|
|
59
60
|
isArray: () => isArray,
|
|
60
61
|
isBoolean: () => isBoolean,
|
|
61
62
|
isFalse: () => isFalse,
|
|
@@ -342,6 +343,9 @@ function ifTrue(val, ifVal, elseVal) {
|
|
|
342
343
|
function isUndefined(i) {
|
|
343
344
|
return typeof i === "undefined";
|
|
344
345
|
}
|
|
346
|
+
function ifUndefined(val, ifVal, elseVal) {
|
|
347
|
+
return isUndefined(val) ? ifVal : elseVal;
|
|
348
|
+
}
|
|
345
349
|
|
|
346
350
|
// src/utility/runtime/type.ts
|
|
347
351
|
var typeApi = () => ({
|
|
@@ -508,10 +512,10 @@ var stringOps = (config, boolLogic) => {
|
|
|
508
512
|
const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
|
|
509
513
|
return combined;
|
|
510
514
|
};
|
|
511
|
-
var filterFn = (defn, logicCombinator,
|
|
515
|
+
var filterFn = (defn, logicCombinator, ifUndefined2 = "no-impact") => {
|
|
512
516
|
const config = isNotFilter(defn) ? defn["not"] : defn;
|
|
513
517
|
const filter2 = (input) => {
|
|
514
|
-
const undefValue =
|
|
518
|
+
const undefValue = ifUndefined2 === "no-impact" ? logicCombinator === "AND" ? true : false : ifUndefined2;
|
|
515
519
|
let flag;
|
|
516
520
|
if (typeof input === "undefined") {
|
|
517
521
|
flag = undefValue;
|
|
@@ -526,8 +530,8 @@ var filterFn = (defn, logicCombinator, ifUndefined = "no-impact") => {
|
|
|
526
530
|
};
|
|
527
531
|
return filter2;
|
|
528
532
|
};
|
|
529
|
-
var filter = (config, logicCombinator = "AND",
|
|
530
|
-
return filterFn(config, logicCombinator,
|
|
533
|
+
var filter = (config, logicCombinator = "AND", ifUndefined2 = "no-impact") => {
|
|
534
|
+
return filterFn(config, logicCombinator, ifUndefined2);
|
|
531
535
|
};
|
|
532
536
|
|
|
533
537
|
// src/utility/dictionary/arrayToKeyLookup.ts
|
|
@@ -904,6 +908,7 @@ function Model(name) {
|
|
|
904
908
|
ifString,
|
|
905
909
|
ifTrue,
|
|
906
910
|
ifTypeOf,
|
|
911
|
+
ifUndefined,
|
|
907
912
|
isArray,
|
|
908
913
|
isBoolean,
|
|
909
914
|
isFalse,
|
package/dist/index.mjs
CHANGED
|
@@ -246,6 +246,9 @@ function ifTrue(val, ifVal, elseVal) {
|
|
|
246
246
|
function isUndefined(i) {
|
|
247
247
|
return typeof i === "undefined";
|
|
248
248
|
}
|
|
249
|
+
function ifUndefined(val, ifVal, elseVal) {
|
|
250
|
+
return isUndefined(val) ? ifVal : elseVal;
|
|
251
|
+
}
|
|
249
252
|
|
|
250
253
|
// src/utility/runtime/type.ts
|
|
251
254
|
var typeApi = () => ({
|
|
@@ -412,10 +415,10 @@ var stringOps = (config, boolLogic) => {
|
|
|
412
415
|
const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
|
|
413
416
|
return combined;
|
|
414
417
|
};
|
|
415
|
-
var filterFn = (defn, logicCombinator,
|
|
418
|
+
var filterFn = (defn, logicCombinator, ifUndefined2 = "no-impact") => {
|
|
416
419
|
const config = isNotFilter(defn) ? defn["not"] : defn;
|
|
417
420
|
const filter2 = (input) => {
|
|
418
|
-
const undefValue =
|
|
421
|
+
const undefValue = ifUndefined2 === "no-impact" ? logicCombinator === "AND" ? true : false : ifUndefined2;
|
|
419
422
|
let flag;
|
|
420
423
|
if (typeof input === "undefined") {
|
|
421
424
|
flag = undefValue;
|
|
@@ -430,8 +433,8 @@ var filterFn = (defn, logicCombinator, ifUndefined = "no-impact") => {
|
|
|
430
433
|
};
|
|
431
434
|
return filter2;
|
|
432
435
|
};
|
|
433
|
-
var filter = (config, logicCombinator = "AND",
|
|
434
|
-
return filterFn(config, logicCombinator,
|
|
436
|
+
var filter = (config, logicCombinator = "AND", ifUndefined2 = "no-impact") => {
|
|
437
|
+
return filterFn(config, logicCombinator, ifUndefined2);
|
|
435
438
|
};
|
|
436
439
|
|
|
437
440
|
// src/utility/dictionary/arrayToKeyLookup.ts
|
|
@@ -807,6 +810,7 @@ export {
|
|
|
807
810
|
ifString,
|
|
808
811
|
ifTrue,
|
|
809
812
|
ifTypeOf,
|
|
813
|
+
ifUndefined,
|
|
810
814
|
isArray,
|
|
811
815
|
isBoolean,
|
|
812
816
|
isFalse,
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { Narrowable } from "../Narrowable";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* **IsUndefined**
|
|
3
5
|
*
|
|
4
6
|
* Boolean type utility returns `true` if `T` is undefined; `false` otherwise
|
|
5
7
|
*/
|
|
6
|
-
export type IsUndefined<T> = T extends undefined ? true : false;
|
|
8
|
+
export type IsUndefined<T extends Narrowable> = T extends undefined ? true : false;
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* **IfUndefined**
|
|
@@ -17,6 +17,10 @@ export function isNull<T extends Narrowable>(i: T) {
|
|
|
17
17
|
* @param ifVal the value (strongly typed) returned if val is `null`
|
|
18
18
|
* @param elseVal the value (strongly typed) returned if val is NOT `null`
|
|
19
19
|
*/
|
|
20
|
-
export function ifNull<T extends Narrowable, IF
|
|
20
|
+
export function ifNull<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(
|
|
21
|
+
val: T,
|
|
22
|
+
ifVal: IF,
|
|
23
|
+
elseVal: ELSE
|
|
24
|
+
) {
|
|
21
25
|
return (isNull(val) ? ifVal : elseVal) as IsNull<T> extends true ? IF : ELSE;
|
|
22
26
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Narrowable } from "src/types";
|
|
2
|
+
|
|
1
3
|
export type IsString<T> = T extends string ? true : false;
|
|
2
4
|
|
|
3
5
|
export function isString<T>(i: T) {
|
|
@@ -15,6 +17,10 @@ export function isString<T>(i: T) {
|
|
|
15
17
|
* @param ifVal the value (strongly typed) returned if val is _string_
|
|
16
18
|
* @param elseVal the value (strongly typed) returned if val is NOT a _string
|
|
17
19
|
*/
|
|
18
|
-
export function ifString<T, IF
|
|
20
|
+
export function ifString<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(
|
|
21
|
+
val: T,
|
|
22
|
+
ifVal: IF,
|
|
23
|
+
elseVal: ELSE
|
|
24
|
+
) {
|
|
19
25
|
return (isString(val) ? ifVal : elseVal) as IsString<T> extends true ? IF : ELSE;
|
|
20
26
|
}
|
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
import { Narrowable } from "src/types";
|
|
2
|
+
import { IsUndefined } from "src/types/TypeInfo/IsUndefined";
|
|
3
|
+
|
|
4
|
+
export function isUndefined<T extends Narrowable>(i: T) {
|
|
2
5
|
return (typeof i === "undefined") as undefined extends T ? true : false;
|
|
3
6
|
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* **ifUndefined**
|
|
10
|
+
*
|
|
11
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
12
|
+
* _undefined_ and returns one of two values (strongly typed) based on the evaluation
|
|
13
|
+
* of this criteria.
|
|
14
|
+
*
|
|
15
|
+
* @param val the value being tested
|
|
16
|
+
* @param ifVal the value (strongly typed) returned if val is `undefined`
|
|
17
|
+
* @param elseVal the value (strongly typed) returned if val is NOT `undefined`
|
|
18
|
+
*/
|
|
19
|
+
export function ifUndefined<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(
|
|
20
|
+
val: T,
|
|
21
|
+
ifVal: IF,
|
|
22
|
+
elseVal: ELSE
|
|
23
|
+
) {
|
|
24
|
+
return (isUndefined(val) ? ifVal : elseVal) as IsUndefined<T> extends true ? IF : ELSE;
|
|
25
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it } from "vitest";
|
|
2
2
|
|
|
3
3
|
import type { Expect, Equal } from "@type-challenges/utils";
|
|
4
|
-
import { ifBoolean, ifNumber, ifString, ifTrue } from "src/utility";
|
|
4
|
+
import { ifBoolean, ifNumber, ifString, ifTrue, ifUndefined } from "src/utility";
|
|
5
5
|
|
|
6
6
|
describe("runtime if/is", () => {
|
|
7
7
|
it("ifString(v,i,e)", () => {
|
|
@@ -49,4 +49,17 @@ describe("runtime if/is", () => {
|
|
|
49
49
|
];
|
|
50
50
|
const cases: cases = [true, true, true];
|
|
51
51
|
});
|
|
52
|
+
|
|
53
|
+
it("ifUndefined(v,i,e)", () => {
|
|
54
|
+
const t = ifUndefined(undefined, 42, false);
|
|
55
|
+
const f = ifUndefined(false, "yikes", 42);
|
|
56
|
+
const f2 = ifUndefined("", "yikes", 42);
|
|
57
|
+
|
|
58
|
+
type cases = [
|
|
59
|
+
Expect<Equal<typeof t, 42>>, //
|
|
60
|
+
Expect<Equal<typeof f, 42>>, //
|
|
61
|
+
Expect<Equal<typeof f2, 42>> //
|
|
62
|
+
];
|
|
63
|
+
const cases: cases = [true, true, true];
|
|
64
|
+
});
|
|
52
65
|
});
|