feat(type-plus)!: reject unknown option keys - #694
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: d76aa71 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #694 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 43 43
Lines 231 231
Branches 51 51
=========================================
Hits 231 231 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Add `IsObject<T, { exact: true }>`, `Assignable<T, object>` and
`Assignable<T, object, { distributive: false }>` so the option paths
have a measured cost next to the no-options fast paths.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S7D6GMwJiCoJGmdnWD9Q3T
Constrain every `$O` options parameter with `$StrictOptions<$O, X.$Options>`, so a misspelled key next to a valid one is an error naming the key and, for a prefix match, suggesting the valid key. Export `$StrictOptions`, `$ErrorMessage`, and `$ForwardOptions`, which narrows a generic wrapper's `$O` to the keys its target accepts. `testType` and `IsAnyOrNever` forward their options through it. BREAKING CHANGE: a generic type that passes its own `$O` to a type-plus type (`type Mine<T, $O extends IsObject.$Options> = IsObject<T, $O>`) no longer compiles. Repeat the strict constraint, or forward with `$ForwardOptions`. See the strict-options guide. Closes #685 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S7D6GMwJiCoJGmdnWD9Q3T
Unknown keys now report "'<key>' is not a valid option", with a suggestion on a prefix match; the generic wrapper constrained only by the options type now errors. Add probes for the two migration shapes that compile and for a typo forwarded through `$ForwardOptions`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S7D6GMwJiCoJGmdnWD9Q3T
unional
force-pushed
the
feat-strict-options
branch
from
September 13, 2026 23:05
415398d to
87c5e24
Compare
…olve
`IsNever`, `IsNotNever`, `IsUnknown`, `IsNotUnknown`, `IsNotStrictFunction`
and `$SelectInvert` (behind `IsNotFunction` and `IsNotNumeric`) resolve a
`$void` branch, but their `$Options` never declared it. The strict option
keys rejected it, so the `void` branch could no longer be overridden.
The strict-key spec, snapshot probe, changeset and guide used
`IsNever<1, { $void: 'V'; $else: 'E' }>` as an invalid key; use
`distributive`, which `IsNever` does not take, and pin `$void` as accepted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S7D6GMwJiCoJGmdnWD9Q3T
…nteger predicates `IsPositive`, `IsNegative`, `IsInteger` and their negations declared `$any`, `$unknown`, `$never` and `$void` but passed only `distributive` and their own branches to `IsBigint`/`IsNumber`, so the options compiled and did nothing. Route through `$Special` when one of those keys is passed, with the previous body as the default for every branch. Without them the type takes the previous body directly: +3 instantiations per use without options, +8 to +10 with other options. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S7D6GMwJiCoJGmdnWD9Q3T
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #685. Part of #689.
What changed
$Ooptions parameter (66 types: predicates,testType,And/Or/Not/Xor,Has*,StringPlus.Includes,ArrayPlus.IsIndexOutOfBound) now constrains it with$StrictOptions<$O, X.$Options>. A misspelled key next to a valid one is an error:$type/utils/$strict_options.ts:$StrictOptions,$ErrorMessage(a string with a zero-width space at the end, the ArkType technique), and$ForwardOptions<$O, A, K>.$ForwardOptionsisPick<$O, Exclude<Extract<keyof $O, keyof A>, K>>. It drops the wrapper's own keys and narrows the rest in one type. TypeScript accepts the result against the strict constraint even while$Ois generic, on all five compilers. There is no conditional, so the no-strip narrowing trap (a silentneverwhen an own option is passed) cannot happen. The spec covers the wrapper's own option, forwarded options, typos in either set, and the trap itself.testing/test_type.tsand 1 inIsAnyOrNever. They are now strict on their own options and forward through$ForwardOptions, sotestTyperejects unknown keys too.guides/strict-options.mdx(added to the sidebar), covers both wrapper shapes, the helper, and the trap. Also updated:never/readme.md(the custom-type example, which was already wrong:$neveris not anIsNeveroption), type-branching, the options reference, the Then/Else migration guide, API signatures, legacy readmes and TSDoc.IsObject+exact,Assignable+objectandAssignable+object+nondistributive.Breaking
type Mine<T, $O extends IsObject.$Options> = IsObject<T, $O>stops compiling (the decision is already accepted for v8). The changeset ismajorand names the break.Instantiations per use (TS 6.0 / TS 7, 300 uses)
IsObject<T>IsObject<T, { exact: true }>IsNever<T>Assignable<T, object>Assignable<T, object, { distributive: false }>The no-options fast paths from #690/#692 still hold: the body is unchanged, and the constraint adds +3 without options and +15/+16 with options. I tried an
as-remapped constraint. It cost more (152 withexact) and broke$ForwardOptions, so I didn't use it.Verification
pnpm --filter type-plus test:type, TS 5.4 / 5.5 / 5.6 / 6.0 / 7: 0 errors, run after each family was converted.pnpm verify:pkggreen (272 test files),pnpm docs:llms:checkup to date, andbiome checkclean except the untracked local harness directory.@ts-expect-errors depend on the constraint: with$StrictOptionsloosened, the mixed-key cases and the lax-wrapper case report "Unused '@ts-expect-error'".Error snapshots (#693)
Rebased onto #693. I updated
error-snapshots/snapshot.txt(identical on all five compilers):misspelled_key_with_valid_keyandkey_invalid_for_type(distributiveonIsNever): no error before. They now report'exactt' is not a valid option. Did you mean 'exact'?and'distributive' is not a valid option.void_branch_on_is_never(IsNever<void, { $void: 'V'; $else: 'E' }>): no error, see below.key_only_typo: was TS2559 "has no properties in common". It now gets the same message and suggestion.wrong_value,assignable_wrong_value,typo_and_wrong_value: same value error, with one added head line ($StrictOptions<…, $Options>).generic_wrapper: now errors, as intended.generic_wrapper_same_constraintandgeneric_wrapper_forwardreport no error.generic_wrapper_forward_typoreports the message.$voidwas a real option#685 cited
IsNever<1, { $void: 'V'; $else: 'E' }>as a wrong key, butIsNeverresolves a$voidbranch (since f09ef4d) and only its$Optionsleft it out. Onmainthat call answered'V'forvoid. The strict keys would have made the branch impossible to override.I checked every single-parameter predicate, plus
Assignable/NotAssignable/Equal/HasKey/IsOptionalKey, with each special key, comparing whether the key changes the result against whether$Optionsdeclares it. Resolved but not declared:$voidonIsNever,IsNotNever,IsUnknown,IsNotUnknown,IsNotStrictFunction, and on$SelectInvert(behindIsNotFunctionandIsNotNumeric). All of them now declare it, with acan override $void branchspec for each.The reverse gap is fixed here too.
IsPositive,IsNegative,IsIntegerand their negations declared$any/$unknown/$never/$voidbut ignored them. They now route through$Specialwhen one of those keys is passed, with the previous body as the default for every branch, so results without those keys are unchanged. There is a spec for each type and a separatepatchchangeset.Cost per use (TS 6.0; TS 7 is within 2.5):
IsPositive<T>IsPositive<T, { selection: 'filter' }>IsNotPositive<T>IsNegative<T>IsNotNegative<T>IsInteger<T>IsNotInteger<T>IsNotInteger<T, { selection: 'filter' }>I also tried a union of key objects (+8 without options) and a key intersection (+6 without options, +6 with
filter) as the guard. I keptExtractbecause the no-options path is the common one.Still open: those six also declare
exactand never read it, althoughapi/number.mdlists it as supported.Generated with Claude Code
https://claude.ai/code/session_01S7D6GMwJiCoJGmdnWD9Q3T