Skip to content

Commit

Permalink
fix(path): fix broken overload for getAtPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Nov 5, 2023
1 parent 477e044 commit 3d0b12f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/path/get/__test__/getAtPath.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expectTypeOf, test} from 'vitest'
import {getAtPath} from '../getAtPath'
import type {Path} from '../../types'
import type {Get, GetAtPath} from '../getAtPath'

test('Get (shallow) typings', () => {
Expand Down Expand Up @@ -74,6 +75,10 @@ test('deepGet() function', () => {

expectTypeOf(getAtPath([], testDoc)).toEqualTypeOf(testDoc)

const path: Path = ['items']

expectTypeOf(getAtPath(path, testDoc)).toEqualTypeOf<unknown>()

expectTypeOf(getAtPath(['items'], testDoc)).toEqualTypeOf(testDoc.items)

expectTypeOf(getAtPath(['nonexistent'], testDoc)).toEqualTypeOf<never>()
Expand Down
9 changes: 5 additions & 4 deletions src/path/get/getAtPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export type GetAtPath<P extends readonly PathElement[], T> = P extends []
: undefined
: undefined

export function getAtPath<T>(path: [], value: T): T
export function getAtPath<const Head extends PathElement, const T>(
path: [head: Head],
value: T,
Expand All @@ -35,12 +34,14 @@ export function getAtPath<
const Tail extends PathElement[],
T,
>(path: [head: Head, ...tail: Tail], value: T): GetAtPath<[Head, ...Tail], T>
export function getAtPath(path: Path, val: unknown): unknown {
export function getAtPath<T>(path: [], value: T): T
export function getAtPath(path: Path, value: unknown): unknown
export function getAtPath(path: Path, value: unknown): unknown {
if (path.length === 0) {
return val
return value
}

let current = val
let current = value
for (const head of path) {
if (isArrayElement(head)) {
if (!Array.isArray(current)) {
Expand Down

0 comments on commit 3d0b12f

Please sign in to comment.