I have following types which I'm trying to get the required path without sacrificing the type. But it throws the following error
export interface IProps {
  user: any;
  car: IVehicle;
}
export interface IVehicle {
 kind: String;
 color: String;
}
_.get<IProps, 'car.color'>(props, 'car.color');
[ts] Argument of type '"car.color"' is not assignable to parameter of type 'number'.
car.color is of type String. So the usage would be:
_.get<IProps, String>(props, 'car.color');
const color = props?.car?.color;
In d.ts file we have such overloading:
get<TObject extends object, TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1]>(object: TObject, path: [TKey1, TKey2]): TObject[TKey1][TKey2];
So it should be:
_.get<IProps, 'car', 'color'>(props, ['car', 'color']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With