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

Fix: path #83

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ expectType<boolean>(path(['one', 'two', 'other'], obj));
const somePath: string[] = ['one', 'two', 'three'];
expectType<unknown>(path(somePath, obj));
// typing the generic will give you a better return, but will always be unions with `unknown`
expectType<Obj['one']['two']['three'] | unknown>(path<Obj['one']['two']['three']>(somePath, obj));
expectType<Obj['one']['two']['three'] | undefined>(path<Obj['one']['two']['three']>(somePath, obj));

// curried type allows for any, and allows you to set the return type in the generic
// notice return type is based just on the generic and is always `| undefined`
Expand Down
4 changes: 2 additions & 2 deletions types/path.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Path } from './util/tools';

// need to evaluate how to do this curried, keep simple type for now
export function path<T>(path: Path): (obj: any) => T | undefined;
export function path<T = unknown>(path: Path): (obj: any) => T | undefined;
// full signatures
export function path<S, K0 extends keyof S>(path: [K0], obj: S): S[K0];
export function path<S, K0 extends keyof S, K1 extends keyof S[K0]>(path: [K0, K1], obj: S): S[K0][K1];
Expand Down Expand Up @@ -69,4 +69,4 @@ export function path<
K8 extends keyof S[K0][K1][K2][K3][K4][K5][K6][K7]
>(path: [K0, K1, K2, K3, K4, K5, K6, K7, K8], obj: S): S[K0][K1][K2][K3][K4][K5][K6][K7][K8];
// final backup
export function path<T>(path: Path, obj: any): T | unknown;
export function path<T = unknown>(path: Path, obj: any): T | undefined;