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

Path: add bracketNotation option #926

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions source/internal/numeric.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ NumberAbsolute<NegativeInfinity>
*/
export type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;

/**
Return a type if it is a number or a number string

@example
```
type A = IsNumberLike<'1'>;
//=> true

type B = IsNumberLike<1>;
//=> true

type C = IsNumberLike<'a'>;
//=> false
*/
export type IsNumberLike<N> =
Emiyaaaaa marked this conversation as resolved.
Show resolved Hide resolved
N extends number ? true
: N extends `${number}` ? true
: false;

/**
Returns the minimum number in the given union of numbers.

Expand Down
74 changes: 59 additions & 15 deletions source/paths.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {StaticPartOfArray, VariablePartOfArray, NonRecursiveType, ToString} from './internal';
import type {StaticPartOfArray, VariablePartOfArray, NonRecursiveType, ToString, IsNumberLike} from './internal';
import type {EmptyObject} from './empty-object';
import type {IsAny} from './is-any';
import type {IsNever} from './is-never';
import type {UnknownArray} from './unknown-array';
import type {Subtract} from './subtract';
import type {GreaterThan} from './greater-than';
import {type Sum} from './sum';

/**
Paths options.
Expand All @@ -18,6 +18,18 @@ export type PathsOptions = {
@default 10
*/
maxRecursionDepth?: number;

/**
Style of the path.

@default 'a.0.b'
*/
style?: 'a[0].b' | 'a.0.b';
Emiyaaaaa marked this conversation as resolved.
Show resolved Hide resolved
Emiyaaaaa marked this conversation as resolved.
Show resolved Hide resolved
};

type DefaultPathsOptions = {
maxRecursionDepth: 10;
style: 'a.0.b';
};

/**
Expand Down Expand Up @@ -61,7 +73,14 @@ open('listB.1'); // TypeError. Because listB only has one element.
@category Object
@category Array
*/
export type Paths<T, Options extends PathsOptions = {}> =
export type Paths<T, Options extends PathsOptions = {}> = _Paths<T, {
// Set default maxRecursionDepth to 10
maxRecursionDepth: Options['maxRecursionDepth'] extends number ? Options['maxRecursionDepth'] : DefaultPathsOptions['maxRecursionDepth'];
// Set default style to 'arr.0.b'
style: Options['style'] extends string ? Options['style'] : DefaultPathsOptions['style'];
}>;

type _Paths<T, Options extends Required<PathsOptions>> =
T extends NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
? never
: IsAny<T> extends true
Expand All @@ -76,25 +95,50 @@ export type Paths<T, Options extends PathsOptions = {}> =
? InternalPaths<T, Options>
: never;

type InternalPaths<T, Options extends PathsOptions = {}> =
(Options['maxRecursionDepth'] extends number ? Options['maxRecursionDepth'] : 10) extends infer MaxDepth extends number
type InternalPaths<T, Options extends Required<PathsOptions>> =
Options['maxRecursionDepth'] extends infer MaxDepth extends number
? Required<T> extends infer T
? T extends EmptyObject | readonly []
? never
: {
[Key in keyof T]:
Key extends string | number // Limit `Key` to string or number.
// If `Key` is a number, return `Key | `${Key}``, because both `array[0]` and `array['0']` work.
?
| Key
| ToString<Key>
| (
GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
? IsNever<Paths<T[Key], {maxRecursionDepth: Subtract<MaxDepth, 1>}>> extends false
? `${Key}.${Paths<T[Key], {maxRecursionDepth: Subtract<MaxDepth, 1>}>}`
: never
? (
Options['style'] extends 'a[0].b'
? IsNumberLike<Key> extends true
? `[${Key}]`
: (Key | ToString<Key>)
: never
)
|
Options['style'] extends 'a.0.b'
// If `Key` is a number, return `Key | `${Key}``, because both `array[0]` and `array['0']` work.
? (Key | ToString<Key>)
: never
) extends infer TranformedKey extends string | number ?
// 1. If style is 'a[0].b' and 'Key' is a numberlike value like 3 or '3', transform 'Key' to `[${Key}]`, else to `${Key}` | Key
// 2. If style is 'a.0.b', transform 'Key' to `${Key}` | Key
| TranformedKey
| (
// Recursively generate paths for the current key
GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
? _Paths<T[Key], {style: Options['style']; maxRecursionDepth: Subtract<MaxDepth, 1>}> extends infer SubPath
? SubPath extends string | number
? (
Options['style'] extends 'a[0].b'
? SubPath extends `[${number}]` | `[${number}]${string}`
? `${TranformedKey}${SubPath}` // If next node is number key like `[3]`, no need to add `.` before it.
: `${TranformedKey}.${SubPath}`
: never
) | (
Options['style'] extends 'a.0.b'
? `${TranformedKey}.${SubPath}`
: never
)
: never
: never
: never
)
: never
: never
}[keyof T & (T extends UnknownArray ? number : unknown)]
: never
Expand Down
30 changes: 29 additions & 1 deletion test-d/paths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectAssignable, expectType} from 'tsd';
import {expectAssignable, expectNotAssignable, expectType} from 'tsd';
import type {Paths} from '../index';

declare const normal: Paths<{foo: string}>;
Expand Down Expand Up @@ -118,3 +118,31 @@ expectType<'foo'>(recursion0);

declare const recursion1: Paths<RecursiveFoo, {maxRecursionDepth: 1}>;
expectType<'foo' | 'foo.foo'>(recursion1);

// Test a[0].b style
type Object1 = {
arr: [{a: string}];
};
expectType<Paths<Object1, {style: 'a[0].b'}>>({} as 'arr' | 'arr[0]' | 'arr[0].a');

type Object2 = {
arr: Array<{a: string}>;
arr1: string[];
};
expectType<Paths<Object2, {style: 'a[0].b'}>>({} as 'arr' | 'arr1' | `arr[${number}]` | `arr[${number}].a` | `arr1[${number}]`);

type Object3 = {
1: 'foo';
'2': 'bar';
};
expectType<Paths<Object3, {style: 'a[0].b'}>>({} as '[1]' | '[2]');

type deepArray = {
arr: Array<Array<Array<{a: string}>>>;
};
expectType<Paths<deepArray, {style: 'a[0].b'}>>({} as 'arr' | `arr[${number}]` | `arr[${number}][${number}]` | `arr[${number}][${number}][${number}]` | `arr[${number}][${number}][${number}].a`);

type RecursionArray = RecursionArray[];
type RecursionArrayPaths = Paths<RecursionArray, {style: 'a[0].b'; maxRecursionDepth: 3}>;
expectAssignable<RecursionArrayPaths>({} as `[${number}][${number}][${number}][${number}]`);
expectNotAssignable<RecursionArrayPaths>({} as `[${number}][${number}][${number}][${number}][${number}]`);