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

Passing a generic to Path does not work #10

Open
dbrxnds opened this issue Mar 1, 2023 · 2 comments
Open

Passing a generic to Path does not work #10

dbrxnds opened this issue Mar 1, 2023 · 2 comments

Comments

@dbrxnds
Copy link

dbrxnds commented Mar 1, 2023

Hi,

It seems that passing a generic which extends something does not correctly resolve at least the extended options:

Reproduction: https://tsplay.dev/NaEx9N

@ErikCoup
Copy link

ErikCoup commented Mar 9, 2023

A related example:

import { getByPath, Path } from 'dot-path-value';

const users = { name: 'Someone', pets: [{ cats: 2 }] };

function getValue<T extends Record<string, any>>(obj: T, path: Path<T>) {
  return getByPath(obj, path);
}

// Here, the type of `cats` is the union of all field types, i.e.:
// `string | number | { cats: number; } | { cats: number; }[]`
const cats = getValue(users, 'pets.0.cats');

@ydhn
Copy link

ydhn commented Sep 30, 2024

Try to do like this:

function getValue<T extends Record<string, any>, TPath extends Path<T>>(
  obj: T,
  path: TPath,
) {
  return getByPath(obj, path);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants