Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript type safe string with dot notation for query nested object

I am working with Typescript and firebase and I have a small abstraction layer with this function to search for a unique document base on its field name and its value.

  where<K extends keyof (T & DocumentEntity)>(fieldName: K, operator: WhereFilterOp, value: unknown): Query<T> {
    this.addCriterion(new WhereCriterion(fieldName as string, operator, value));
    return this;
  }

This works well when I want to query with a field at the base of the document, for example:

Model:

order: Order = {
  orderId: baseId
  item: { ... }
  price: { ... }
  restaurant: {
    restaurantId: nestedId
    name: chezGaston
  }
}

Query:

    const order = await this.documentPersistence.findUnique(
      new Query<order>().where('orderId', '==', incomingOrderId)
    );

But now I want to query base on the id of a nested object.

const order = await this.documentPersistence.findUnique(
      new Query<order>()
        .where('restaurant.restaurantId', '==', integration),
    );

And this gives me a static error TS2345: Argument of type '"restaurant.restaurantId"' is not assignable to parameter of type 'keyof Order'.

How can I fix my function so it accepts Nested object as keyof my object?

I don't want to use // @ts-ignore

like image 637
François Duguay-Giguère Avatar asked Aug 25 '26 09:08

François Duguay-Giguère


1 Answers

You can do this as of TypeScript 4.1.

Click the playground example to see it in action:

TypeScript Playground

Original Twitter Post

Here's the relevant code:

type PathImpl<T, K extends keyof T> =
  K extends string
  ? T[K] extends Record<string, any>
    ? T[K] extends ArrayLike<any>
      ? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}`
      : K | `${K}.${PathImpl<T[K], keyof T[K]>}`
    : K
  : never;

type Path<T> = PathImpl<T, keyof T> | keyof T;

type PathValue<T, P extends Path<T>> =
  P extends `${infer K}.${infer Rest}`
  ? K extends keyof T
    ? Rest extends Path<T[K]>
      ? PathValue<T[K], Rest>
      : never
    : never
  : P extends keyof T
    ? T[P]
    : never;

declare function get<T, P extends Path<T>>(obj: T, path: P): PathValue<T, P>;

const object = {
  firstName: "Diego",
  lastName: "Haz",
  age: 30,
  projects: [
    { name: "Reakit", contributors: 68 },
    { name: "Constate", contributors: 12 },
  ]
} as const;

get(object, "firstName"); // works
get(object, "projects.0"); // works
get(object, "projects.0.name"); // works

get(object, "role"); // type error

like image 54
jeremiahmontoya Avatar answered Aug 28 '26 04:08

jeremiahmontoya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!