Skip to content

[Types] Query with dotted paths #12064

Open
@hasezoey

Description

@hasezoey

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Currently Query's do not have dotted path suggestions, but would be possible with current typescript.

See This Stackoverflow Answer

TL;DR:
a simple example of what the stackoverflow answer has does (note: the types are directly copied from the answer)

interface Test {
  somenested?: {
    path: string;
  };
  somethingelse?: string;
}

type PathsToStringProps<T> = T extends string
  ? []
  : {
      [K in Extract<keyof T, string>]: [K, ...PathsToStringProps<T[K]>];
    }[Extract<keyof T, string>];

type Join<T extends string[], D extends string> = T extends []
  ? never
  : T extends [infer F]
  ? F
  : T extends [infer F, ...infer R]
  ? F extends string
    ? `${F}${D}${Join<Extract<R, string[]>, D>}`
    : never
  : string;

function onlyDotted(input: Join<PathsToStringProps<Test>, '.'>) {
  console.log('input', input);
}

onlyDotted('somenested.path'); // works, with suggestions
onlyDotted('somethingelse'); // also works, with suggestions
onlyDotted('ERROR'); // error, property does not exist

Disclaimer: i am by no means a expert at complex typescript types, i also have no clue about the effect on types performance this would bring

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new featuretypescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions