Skip to content

Commit

Permalink
Get: Update Path generic parameter (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa authored Aug 19, 2024
1 parent 2967fe6 commit 986faba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 11 additions & 4 deletions source/get.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {StringDigit} from './internal';
import type {StringDigit, ToString} from './internal';
import type {LiteralStringUnion} from './literal-union';
import type {Paths} from './paths';
import type {Split} from './split';
import type {StringKeyOf} from './string-key-of';

Expand All @@ -16,7 +18,7 @@ type GetOptions = {
/**
Like the `Get` type but receives an array of strings as a path parameter.
*/
type GetWithPath<BaseType, Keys extends readonly string[], Options extends GetOptions = {}> =
type GetWithPath<BaseType, Keys, Options extends GetOptions = {}> =
Keys extends readonly []
? BaseType
: Keys extends readonly [infer Head, ...infer Tail]
Expand Down Expand Up @@ -187,5 +189,10 @@ Get<Record<string, string>, 'foo', {strict: true}> // => string
@category Array
@category Template literal
*/
export type Get<BaseType, Path extends string | readonly string[], Options extends GetOptions = {}> =
GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
export type Get<
BaseType,
Path extends
| readonly string[]
| LiteralStringUnion<ToString<Paths<BaseType, {bracketNotation: false}> | Paths<BaseType, {bracketNotation: true}>>>,
Options extends GetOptions = {}> =
GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
2 changes: 2 additions & 0 deletions source/literal-union.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {Primitive} from './primitive';

export type LiteralStringUnion<T> = LiteralUnion<T, string>;

/**
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
Expand Down

0 comments on commit 986faba

Please sign in to comment.