Ts-toolbelt: Object.path doesn't handle nullable and optional values correctly at last hop.

Created on 19 Feb 2020  路  3Comments  路  Source: millsp/ts-toolbelt

馃悶 Bug Report

Describe the bug

Object.path doesn't handle nullable and optional values correctly at last hop.

Reproduce the bug

Object.Path<{nullable: number | null | undefined}, ['nullable']>

Expected behavior

Expected code above to return number | null | undefined, since this is the value at the path. Returns number in v6.3.0

bug enhancement

All 3 comments

I've tried swapping the order of operations between NonNullable and At, which I believe solves this problem, but it causes tests for O.MergeUp and O.Nullable to fail and I'm not sure I understand why.

type __Path<O, Path extends List<Key>, strict extends Boolean, I extends Iteration = IterationOf<'0'>> = {
-    0: __Path<NonNullable<At<O & {}, Path[Pos<I>], strict>>, Path, strict, Next<I>>
+    0: __Path<At<NonNullable<O> & {}, Path[Pos<I>], strict>, Path, strict, Next<I>>
    1: O // Use of `NonNullable` otherwise path cannot be followed #`undefined`
}[Extends<Pos<I>, Length<Path>>]

Hi @smoogly, thanks for reporting this. There's a strict option that is supposed to handle that:

type test = Path<{nullable: number | null | undefined}, ['nullable'], 0>

But it does not behave like you said. So I'm going to fix this.

Your fix is out in a few minutes, thanks!

Was this page helpful?
0 / 5 - 0 ratings