Skip to content

Add ConditionalPaths type#1485

Open
naman-luthra-rubrik wants to merge 2 commits into
sindresorhus:mainfrom
naman-luthra-rubrik:add-conditional-paths
Open

Add ConditionalPaths type#1485
naman-luthra-rubrik wants to merge 2 commits into
sindresorhus:mainfrom
naman-luthra-rubrik:add-conditional-paths

Conversation

@naman-luthra-rubrik

Copy link
Copy Markdown

This PR:

  • Adds ConditionalPaths<Type, Condition, Options> - the path-aware counterpart of ConditionalKeys. Returns a union of all dot-notation paths in an object whose value type satisfies Condition.
  • Closes Proposal: ConditionalPaths #1328

Supports:

  • extends mode (default): value assignable to Condition
  • equality mode: exact type match via IsEqual
  • All Paths options except leavesOnly (which is a no-op for any specific-type condition since primitive-typed fields are always leaves)

naman-luthra-rubrik and others added 2 commits July 7, 2026 19:18
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@sindresorhus

Copy link
Copy Markdown
Owner

This is described as the path-aware counterpart of ConditionalKeys, but the semantics differ. Optional properties are treated as required before checking the condition, so {name?: string} filtered by string includes name. ConditionalKeys requires string | undefined for that.

never properties also match evry condition because of the tuple-wrapped extends check, so {data: never; name: string} filtered by string includes both paths. That is suprising, and ConditionalKeys already avoids this with ExtendsStrict.

I'm also not convinced the new type is worth duplicating most of the path-generation logic. If we add this, I think it should be a thin composition over Paths, Get, and ExtendsStrict, unless there is a benchmark showing that approach is too slow.

I feel like this is a bit too niche

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.

Proposal: ConditionalPaths

2 participants