Skip to content

Add OptionalPaths type#1443

Open
som-sm wants to merge 1 commit into
mainfrom
feat/add-optional-paths-type
Open

Add OptionalPaths type#1443
som-sm wants to merge 1 commit into
mainfrom
feat/add-optional-paths-type

Conversation

@som-sm

@som-sm som-sm commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Closes #1426

@som-sm som-sm force-pushed the feat/add-optional-paths-type branch from 70c4d39 to a488dbb Compare June 2, 2026 14:57
Repository owner deleted a comment from claude Bot Jun 2, 2026
@som-sm som-sm force-pushed the feat/add-optional-paths-type branch from a488dbb to ef1fa9e Compare June 2, 2026 15:03
Repository owner deleted a comment from claude Bot Jun 2, 2026
@som-sm som-sm force-pushed the feat/add-optional-paths-type branch from ef1fa9e to a8711d4 Compare June 2, 2026 15:06
Repository owner deleted a comment from claude Bot Jun 2, 2026
Repository owner deleted a comment from claude Bot Jun 2, 2026
@som-sm som-sm force-pushed the feat/add-optional-paths-type branch from a8711d4 to d72f38e Compare June 2, 2026 15:22
Repository owner deleted a comment from claude Bot Jun 2, 2026
@som-sm som-sm marked this pull request as ready for review June 2, 2026 15:28
@som-sm som-sm requested a review from sindresorhus June 2, 2026 15:28
@sindresorhus

Copy link
Copy Markdown
Owner

the new type says it can be used with PickDeep/OmitDeep, but that does not hold for all of the documented output. For example:

type Tuple = [string, number?];
type Result = OmitDeep<Tuple, OptionalPaths<Tuple>>;

OptionalPaths<Tuple> includes 1 | '1', while the deep path helpers only accept the string tuple path here. So the numeric member makes the composition fail. I think the simplest fix is to make the public result only produce paths that are accepted by the existing deep helpers, instead of returning extra numeric tuple variants.

There is also a generic version of the same problem, which is close to the original use case from the issue:

type MinimalObject<T extends object> = OmitDeep<T, OptionalPaths<T>>;

That currently fails the path constraint. Extract<OptionalPaths<T>, Paths<T>> makes the object case work, but users should not need that if the documented use case is direct composition with OmitDeep.

and I would add tests for:

type RequiredUserFields = OmitDeep<UserFields, OptionalPaths<UserFields>>;
type OptionalUserFields = PickDeep<UserFields, OptionalPaths<UserFields>>;
type MinimalObject<T extends object> = OmitDeep<T, OptionalPaths<T>>;

And if array support stays in scope, also add a tuple composition test. Right now the standalone OptionalPaths tests are pretty thorough, but they miss whether the type actually compsoes with the helpers it is meant to feed into.

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

Successfully merging this pull request may close these issues.

OmitOptional and OmitOptionalDeep: Removes non-required keys from an object.

2 participants