Ts-toolbelt: strange behavior with PathValid

Created on 25 Jul 2019  路  2Comments  路  Source: millsp/ts-toolbelt

Hi @pirix-gh

First I'm glad to see you finally released v3!

I have the following problem: (using v3)

import { Object, Any, Union } from 'ts-toolbelt';

export function getProp<O extends object, P extends Array<string | number>>(
  obj: Union.Nullable<O>,
  ...keys: Any.Cast<P, Object.PathValid<O, P>>
): Object.Path<O, P> | undefined {
  return keys.reduce(
    (result: any, key: string | number | symbol) => (result === null || result === undefined ? undefined : result[key]),
    obj,
  );
}

interface IInterface {
  a: {
    b: string[];
  };
}

type elem = IInterface['a']['b'][1]; // string
const a: IInterface = undefined as any;
const res = getProp(a, 'a', 'b'); // string[] | undefined
const res = getProp(a, 'a', 'b', 0); // TS2345: Argument of type '0' is not assignable to parameter of type 'never'.

Any thoughts?

bug

All 2 comments

Hi @regevbr, glad to see you again!

I see that the curse of the PathValid is still following us.

My bad, I was casting the type parameter O by doing O & [] instead of O & {}.

It's been fixed! And many thanks for reporting bugs :tada:

@pirix-gh thanks it is working now!

Was this page helpful?
0 / 5 - 0 ratings