Comparing version 2.5.0 to 2.5.1
146
lib/idx.d.ts
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
interface _DeepRequiredArray<T> | ||
interface DeepRequiredArray<T> | ||
extends Array<DeepRequired<NonUndefinedOrnNull<T>>> {} | ||
@@ -19,3 +19,3 @@ | ||
*/ | ||
type _DeepRequiredObject<T> = { | ||
type DeepRequiredObject<T> = { | ||
[P in keyof T]-?: DeepRequired<NonUndefinedOrnNull<T[P]>> | ||
@@ -25,2 +25,134 @@ }; | ||
/** | ||
* Function that has deeply required return type | ||
*/ | ||
type FunctionWithRequiredReturnType< | ||
T extends (...args: any[]) => any | ||
> = T extends FunctionWithRequiredReturnType_RestArgs<T> | ||
? FunctionWithRequiredReturnType_RestArgs<T> | ||
: T extends FunctionWithRequiredReturnType_Args1<T> | ||
? FunctionWithRequiredReturnType_Args1<T> | ||
: T extends FunctionWithRequiredReturnType_Args2<T> | ||
? FunctionWithRequiredReturnType_Args2<T> | ||
: T extends FunctionWithRequiredReturnType_Args3<T> | ||
? FunctionWithRequiredReturnType_Args3<T> | ||
: T extends FunctionWithRequiredReturnType_Args4<T> | ||
? FunctionWithRequiredReturnType_Args4<T> | ||
: T extends FunctionWithRequiredReturnType_Args5<T> | ||
? FunctionWithRequiredReturnType_Args5<T> | ||
: T extends FunctionWithRequiredReturnType_Args6<T> | ||
? FunctionWithRequiredReturnType_Args6<T> | ||
: T extends FunctionWithRequiredReturnType_Args7<T> | ||
? FunctionWithRequiredReturnType_Args7<T> | ||
: T extends FunctionWithRequiredReturnType_NoArgs<T> | ||
? FunctionWithRequiredReturnType_NoArgs<T> | ||
: FunctionWithRequiredReturnType_AnyArgs<T>; | ||
/** | ||
* Function that has deeply required return type with no arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_AnyArgs< | ||
T extends (...args: any[]) => any | ||
> = T extends (...args: any[]) => infer R | ||
? (...args: any[]) => DeepRequired<R> | ||
: never; | ||
/** | ||
* Function that has deeply required return type with rest argument | ||
*/ | ||
type FunctionWithRequiredReturnType_RestArgs< | ||
T extends (...args: any[]) => any | ||
> = T extends (...args: infer A) => infer R | ||
? (...args: A) => DeepRequired<R> | ||
: never; | ||
/** | ||
* Function that has deeply required return type with no arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_NoArgs< | ||
T extends () => any | ||
> = T extends () => infer R ? () => DeepRequired<R> : never; | ||
/** | ||
* Function that has deeply required return type with 1 argument | ||
*/ | ||
type FunctionWithRequiredReturnType_Args1< | ||
T extends (arg: any) => any | ||
> = T extends (a: infer A) => infer R ? (a: A) => DeepRequired<R> : never; | ||
/** | ||
* Function that has deeply required return type with 2 arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_Args2< | ||
T extends (arg: any) => any | ||
> = T extends (a: infer A, b: infer B) => infer R | ||
? (a: A, b: B) => DeepRequired<R> | ||
: never; | ||
/** | ||
* Function that has deeply required return type with 3 arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_Args3< | ||
T extends (arg: any) => any | ||
> = T extends (a: infer A, b: infer B, c: infer C) => infer R | ||
? (a: A, b: B, c: C) => DeepRequired<R> | ||
: never; | ||
/** | ||
* Function that has deeply required return type with 4 arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_Args4< | ||
T extends (arg: any) => any | ||
> = T extends (a: infer A, b: infer B, c: infer C, d: infer D) => infer R | ||
? (a: A, b: B, c: C, d: D) => DeepRequired<R> | ||
: never; | ||
/** | ||
* Function that has deeply required return type with 5 arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_Args5< | ||
T extends (arg: any) => any | ||
> = T extends ( | ||
a: infer A, | ||
b: infer B, | ||
c: infer C, | ||
d: infer D, | ||
e: infer E, | ||
) => infer R | ||
? (a: A, b: B, c: C, d: D, e: E) => DeepRequired<R> | ||
: never; | ||
/** | ||
* Function that has deeply required return type with 6 arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_Args6< | ||
T extends (arg: any) => any | ||
> = T extends ( | ||
a: infer A, | ||
b: infer B, | ||
c: infer C, | ||
d: infer D, | ||
e: infer E, | ||
f: infer F, | ||
) => infer R | ||
? (a: A, b: B, c: C, d: D, e: E, f: F) => DeepRequired<R> | ||
: never; | ||
/** | ||
* Function that has deeply required return type with 7 arguments | ||
*/ | ||
type FunctionWithRequiredReturnType_Args7< | ||
T extends (arg: any) => any | ||
> = T extends ( | ||
a: infer A, | ||
b: infer B, | ||
c: infer C, | ||
d: infer D, | ||
e: infer E, | ||
f: infer F, | ||
g: infer G, | ||
) => infer R | ||
? (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => DeepRequired<R> | ||
: never; | ||
/** | ||
* DeepRequired | ||
@@ -30,4 +162,6 @@ * Required that works for deeply nested structure | ||
type DeepRequired<T> = T extends any[] | ||
? _DeepRequiredArray<T[number]> | ||
: T extends object ? _DeepRequiredObject<T> : T; | ||
? DeepRequiredArray<T[number]> | ||
: T extends (...args: any[]) => any | ||
? FunctionWithRequiredReturnType<T> | ||
: T extends object ? DeepRequiredObject<T> : T; | ||
@@ -62,4 +196,4 @@ /** | ||
* | ||
* @param prop - Parent Object | ||
* @param accessor - Accessor function than c | ||
* @param prop - Parent object | ||
* @param accessor - Accessor function | ||
* @return the property accessed if accessor function could reach to property, | ||
@@ -66,0 +200,0 @@ * null or undefined otherwise |
@@ -39,1 +39,23 @@ import idx from './idx'; | ||
let baz: string | null | undefined = idx(nullable, _ => _.foo.bar.baz); | ||
interface WithMethods<T = any> { | ||
foo?: { | ||
bar?(): number; | ||
}; | ||
baz?: { | ||
fn?(): {inner?: string}; | ||
}; | ||
args?(a: number): number; | ||
manyArgs?(a: number, b: string, c: boolean): number; | ||
restArgs?(...args: string[]): string; | ||
genrric?(arg: T): T; | ||
} | ||
let withMethods: WithMethods<boolean> = {} as any; | ||
let n: number | undefined | null = idx(withMethods, _ => _.foo.bar()); | ||
n = idx(withMethods, _ => _.args(1)); | ||
n = idx(withMethods, _ => _.manyArgs(1, 'b', true)); | ||
let s: string | undefined | null = idx(withMethods, _ => _.baz.fn().inner); | ||
s = idx(withMethods, _ => _.restArgs('1', '2', '3', '4', '5', '6')); | ||
let b: boolean | undefined | null = idx(withMethods, _ => _.genrric(true)); |
{ | ||
"name": "idx", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"description": "Utility function for traversing properties on objects and arrays.", | ||
@@ -16,3 +16,4 @@ "main": "lib/idx.js", | ||
"prepublish": "yarn run build && cp ../../README.md .", | ||
"test": "jest" | ||
"test": "jest && yarn run tsc", | ||
"tsc": "tsc --noEmit --strict" | ||
}, | ||
@@ -19,0 +20,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13614
317