Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15260 - Tree path array #18292

Open
baian1 opened this issue Oct 30, 2022 · 1 comment
Open

15260 - Tree path array #18292

baian1 opened this issue Oct 30, 2022 · 1 comment
Labels
15260 answer Share answers/solutions to a question en in English

Comments

@baian1
Copy link

baian1 commented Oct 30, 2022

type Path<T> = T extends Record<PropertyKey, unknown>
  ? {
      [P in keyof T]: [P, ...Path<T[P]>] | [P];
    }[keyof T]
  : never;
@baian1 baian1 added answer Share answers/solutions to a question en in English labels Oct 30, 2022
@github-actions github-actions bot added the 15260 label Oct 30, 2022
@handletastic
Copy link

handletastic commented May 6, 2023

I have been trying to solve a problem like this for a while and just tested your solution.
I wonder if there is a way to make it so that the second value in the array is restricted instead of suggesting non valid values as well as limit the number of items in the array that are actually valid given the path choice:

type User = {
    name: string;
    age: number;
    address: {
        street: string;
        city: string;
    };
    profession: {
        title: string;
        salary: number;
        experience: {
            years: number;
        };
    };
    // friends: ['Bob', 'Jane', 'John'];
};

type Path<T> = T extends Record<PropertyKey, unknown>
    ? {
        [P in keyof T]: [P, ...Path<T[P]>] | [P];
    }[keyof T]
    : never;

const r: Path<User> = ['profession'];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
15260 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

2 participants