-
-
Notifications
You must be signed in to change notification settings - Fork 601
Change Paths
option maxRecursionDepth
default value
#1029
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
base: main
Are you sure you want to change the base?
Conversation
Paths
option maxRecursionDepth
default value.Paths
option maxRecursionDepth
default value
@@ -53,7 +53,7 @@ export type PathsOptions = { | |||
}; | |||
|
|||
type DefaultPathsOptions = { | |||
maxRecursionDepth: 10; | |||
maxRecursionDepth: 5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Emiyaaaaa This only affects cases where depth is greater than 5, right?
And this would also be a breaking change for cases where depth is greater than 5, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
breaking change
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only affects cases where depth is greater than 5
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea, similar to what is mentioned in the description, is that when the depth recursion reaches a certain number (I think it can be 5), it can be assumed that the user input type is an infinite nested type and we can stop the recursion early, the previous 10 is ts maximum limit of recursion for our implementation code
If that's okay, I need to figure out how to explain the a option in the comments.
This is a breaking change and should probably be done in v5, right? |
yes |
I have a similar idea for this - the main thing I want to acheive is separate the general maximum depth Once that's done we could keep the general depth larger (e.g. just keep it at 10), but reduce the circular depth to something more aggressive (like 3) for performance. This way we would achieve best of both worlds: type SimpleDeep = {a: {b: {c: {d: {e: {f: {g: {h: string}}}}}}}}
Paths<SimpleDeep> => 'a' | 'a.b' | 'a.b.c' | .... | 'a.b.c.d.e.f.g.h'
type DeepWithCircular = {
a: {b: {c: {d: {e: {f: {g: {h: string}}}}}}};
circular: DeepWithCircular;
}
// Let's say we have 3 as default maxCircularDepth
Paths<DeepWithCircular> =>
| 'a' | 'a.b' | 'a.b.c' | .... | 'a.b.c.d.e.f.g.h'
// Note: will not type out 'a' | 'a.b' | 'a.b.c' .... for the ones below but it would have these paths as well
| 'circular'
| 'circular.a.b.c.d.e.f.g.h'
| 'circular.circular'
| 'circular.circular.a.b.c.d.e.f.g.h'
| 'circular.circular.circular'
| 'circular.circular.circular.a.b.c.d.e.f.g' // We hit the default maxRecursionDepth of 10 here
| 'circular.circular.circular.circular'
// We can set a specific maxCircularDepth
Paths<DeepWithCircular, {maxCircularDepth: 1}> =>
| 'a' | 'a.b' | 'a.b.c' | .... | 'a.b.c.d.e.f.g.h'
// Note: will not type out 'a' | 'a.b' | 'a.b.c' .... for the ones below but it would have these paths as well
| 'circular'
| 'circular.a.b.c.d.e.f.g.h'
| 'circular.circular' I started working on this last week, but jumped on Thoughts? @som-sm @sindresorhus @Emiyaaaaa |
Actually, my idea is to limit all default depths, assuming that each attribute has |
@Emiyaaaaa The output you're referring to applies to perfect k-ary trees, but IMO, real-world objects are rarely that perfect. For e.g., the structure below would have
However, real-world objects would often have many nodes missing, something like:
In this case, the total number of paths is only
I agree that reducing |
I agree! In cases involving both circular and non-circular paths, an option like While this would still be a breaking change, it would likely go unnoticed in most cases. Because in cases like the following, it'd be rare that people rely on deep chains like type FileSystemNode = {
name: string;
type: string;
parent?: FileSystemNode;
children?: FileSystemNode[];
}; It’s probably still best to introduce it in v5! |
@som-sm OK, I'll set up the PR to add My plan was to default it to 10 in type PickDeepOptions = Pick<PathsOptions, 'maxCircularDepth'>;
export type PickDeep<T, PathUnion extends Paths<T, Options>, Options extends PickDeepOptions = {maxCircularDepth: 5}> = .... This would further reduce any possible impact or breaking changes as it would only affect Do you think this would still need to wait for v5? |
For performance improvements in most cases, I think we could reduce the default values of
maxRecursionDepth
PS: In TypeScript, also have a similar function to determine whether a type is deeply-nested or not, that value is

5
(reduced to3
after [email protected])