dot-diver icon indicating copy to clipboard operation
dot-diver copied to clipboard

Enhance typing of PathValue for unions.

Open djfhe opened this issue 2 years ago • 0 comments

Let's consider the following cases:

type TestType = {
  a: number | { nested: string }
}

type value = PathValue<TestType, 'a.nested'> // is string

Here, the type of value should be undefined | string, since we cannot determine if a is a number or not.

In this case:

type TestType = {
  a: string | { nested: string }
}

type value = PathValue<TestType, 'a.nested'>  // is string

string is the correct type, as we can access individual characters of a string using index notation.

It might be necessary to split the PathValue type into two distinct types, one for getByPath and another for setByPath, as suggested in this issue: https://github.com/clickbar/dot-diver/issues/3. Additionally, we may need to exclude paths of objects in unions from setByPath, since we cannot determine at runtime whether it is an object or a primitive.

djfhe avatar Mar 26 '23 13:03 djfhe