Skip to content

Commit

Permalink
Updated types
Browse files Browse the repository at this point in the history
  • Loading branch information
incetarik committed Aug 4, 2022
1 parent 2e2f47f commit 0352659
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/dynamic-navigations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Type } from '@nestjs/common'
import type { Type } from '@nestjs/common'

import { toCamelCase } from './helpers'

import type { KeysMatching } from './types'

let _navigations: INavigationMap[] | undefined

/**
Expand Down
4 changes: 3 additions & 1 deletion src/dynamic-resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLResolveInfo } from 'graphql'
import type { GraphQLResolveInfo } from 'graphql'

import { Inject, Type } from '@nestjs/common'
import {
Expand All @@ -23,6 +23,8 @@ import {
toCamelCase,
} from './helpers'

import type { Dictionary } from './types'

let _resolverParams: IRegisterDynamicResolverParams[] | undefined

export interface IOnResolvingParams<P extends object = object, TRoot extends object = object> {
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/extract-grapql-selections.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FieldNode, GraphQLResolveInfo, Kind } from 'graphql'

import type { ObjectKeys, KeysMatching } from '../types'

/**
* Defines how the fields should be renamed/mapped.
*
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import '../types'

export * from './dynamic-navigations'

export {
extractGraphQLSelectionPath,
extractGraphQLSelections,
Expand Down
27 changes: 16 additions & 11 deletions types.d.ts → src/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
/**
* The function interface.
*/
export type Func<A extends any[] = any[], R = any> = (...args: A) => R

/**
* Gets the keys of type `T` matching the `V` values.
*/
declare type KeysMatching<T, V> = { [ K in keyof T ]-?: T[ K ] extends V ? K : never }[ keyof T ]
export type KeysMatching<T, V> = { [ K in keyof T ]-?: T[ K ] extends V ? K : never }[ keyof T ]

/**
* A Dictionary type for keeping `TValue` values.
*/
declare type Dictionary<TValue> = { [ key: string ]: TValue }
export type Dictionary<TValue> = { [ key: string ]: TValue }

/**
* Maps the given type to its function keys.
* If the given type is an array, then the function indices will be returned.
* If the given type is an object, then the function keys will be returned.
*/
declare type FunctionKeys<T>
export type FunctionKeys<T>
= T extends any[]
? Exclude<_FunctionKeys<T>, -1>
: T extends { [ key: string ]: any }
Expand All @@ -23,43 +28,43 @@ declare type FunctionKeys<T>
/**
* Defines the nullable version of given type.
*/
declare type Nullable<T> = T | null | undefined
export type Nullable<T> = T | null | undefined

/**
* Defines the nullable version of given type with `void` type.
*/
declare type NullableReturn<T> = Nullable<T> | void
export type NullableReturn<T> = Nullable<T> | void

/**
* Defines a type which can be in a promise or not.
*/
declare type MaybePromise<T> = Promise<T> | T
export type MaybePromise<T> = Promise<T> | T

/**
* Gets the keys of an object whose values are objects.
*
* This does not include function keys.
*/
declare type ObjectKeys<T extends object>
export type ObjectKeys<T extends object>
= Exclude<KeysMatching<T, object>, FunctionKeys<T>>

/**
* Gets the length of an array.
*/
declare type Length<T extends any[]> = T extends { length: infer L } ? L : never
export type Length<T extends any[]> = T extends { length: infer L } ? L : never

/**
* Builds a tuple with given length.
*/
declare type BuildTuple<L extends number, T extends any[] = []>
export type BuildTuple<L extends number, T extends any[] = []>
= T extends { length: L } ? T : BuildTuple<L, [ ...T, any ]>

type _FunctionKeys<TArray extends any[], CurrentIndex = 0, Index = -1>
type _FunctionKeys<TArray extends any[], CurrentIndex extends number = 0, Index = -1>
= TArray extends [ infer H, ...infer T ]
? H extends Func
? _FunctionKeys<T, Add<CurrentIndex, 1>, Index | CurrentIndex>
: _FunctionKeys<T, Add<CurrentIndex, 1>, Index>
: Index

type Add<A extends number, B extends number> =
Length<[ ...BuildTuple<A>, ...BuildTuple<B> ]>
Length<[ ...BuildTuple<A>, ...BuildTuple<B> ]> & number
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipDefaultLibCheck": true,
"skipLibCheck": true
"skipLibCheck": true,
"lib": [
"ES2015"
]
},
"exclude": [
"node_modules",
Expand Down

0 comments on commit 0352659

Please sign in to comment.