Skip to content

feat(type): add != comparator - #1692

Open
jkugsiya wants to merge 2 commits into
arktypeio:mainfrom
jkugsiya:feat/not-equal-comparator
Open

jkugsiya wants to merge 2 commits into
arktypeio:mainfrom
jkugsiya:feat/not-equal-comparator

Conversation

@jkugsiya

@jkugsiya jkugsiya commented Oct 13, 2026 •

Copy link
Copy Markdown

Fixes #925

Problem

There is no comparator to exclude a single value. number!=0 fails with 'number!' is unresolvable, because ! doesn't end a name. You have to write number<0 | number>0 by hand.

Fix

I added a != comparator. As discussed on the issue, it reduces to the existing union, so number!=0 has the same representation as number<0 | number>0. Intersections, extends and JSON Schema output work as before.

Like ==, it works on everything comparable:

  • numbers: number!=0 is number<0 | number>0
  • string and array lengths: string!=3 is string<3 | string>3. No length is below 0, so string!=0 is string>0.
  • Dates: Date!=d'2020-01-01'

Only != ends a name, so a lone ! in brand names and aliases, like string#a!b, still parses. After a complete operand, a lone !, as in (number)!0, gives '!' is not allowed here. != can't pair with a left bound, so 0!=number gives the existing unpairable comparator error.

If an existing bound rules out one side, that side is dropped. With nonNegative: "number>=0" in a scope, nonNegative!=0 is number>0. If both sides are ruled out, you get the usual unsatisfiable error.

Errors list both branches, for example must be negative or positive (was 0). A dedicated != node could follow in a separate PR for clearer messages.

I also added != to the primitives docs, the cheat sheet and the skill.

Notes

  • With numberAllowsNaN, NaN fails number!=0, since it fails both branches.
  • number%2!=0 means "divisible by 2 and not 0", not "odd".
  • feat(type): support File size bounds #1637 also edits getBoundKinds. If it lands first, File!=N needs the same != branch.

Tests

I added a != block to range.test.ts covering numbers, lengths, Dates, intersections, extends, JSON Schema, bounds that rule out one side, aliases with !, and every parse error. brand.test.ts covers brand names with ! and a brand followed by !=. The new tests fail on main and pass with this change. pnpm prChecks passes locally, and the branch is up to date with main.

Parse number!=0 as number<0 | number>0, so equal types share one
representation. It works on numbers, string and array lengths, and Dates.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Chaining != after an existing bound throws on satisfiable expressions: each disjunct is constrained eagerly, so an empty branch aborts the parse (e.g. number>=0!=0 throws instead of reducing to number>0). Details inline.

Reviewed changes

  • Parser: != comparator — "!=" added to Comparator, comparators, and invertedComparators, and "!" added to comparatorStartChars / terminatingChars, plus a shiftComparator / parseBound error path for a lone !.
  • Runtime reduction — parseRightBound reduces != to a union of exclusive bounds (number<0 | number>0) and getBoundKinds gained != handling for numbers, string/array lengths (with a limit <= 0 single-minLength branch), and Dates.
  • Tests, docs, skill — a != block in range.test.ts (numbers, lengths, Dates, intersections, extends, JSON Schema, parse errors) plus primitives, the cheat sheet, llms.txt, and the SKILL.md table.

I ran pnpm testFiles ark/type/__tests__/range.test.ts (72 passing) and pnpm test (1804 passing) on the branch; both pass.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

Comment on lines +199 to +203
s.root = boundKinds
.map(kind =>
previousRoot.constrain(kind, { rule: limit, exclusive: true })
)
.reduce((l, r) => l.rawOr(r))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boundKinds.map(kind => previousRoot.constrain(...)) constrains every disjunct eagerly, but constrain throws when the intersection is disjoint. So when an existing bound already rules out one side of the excluded value, the whole parse aborts even though the union is satisfiable — type("number>=0!=0") throws Intersection of >= 0 and < 0 results in an unsatisfiable type instead of reducing to number>0. The PR's intersection with min test only exercises the .and(...) form, which never re-enters parseRightBound, so this path is uncovered.

Technical details
# Eager `constrain` throws on an empty `!=` disjunct

## Affected sites
- `ark/type/parser/shift/operator/bounds.ts:201` — `previousRoot.constrain(kind, { rule: limit, exclusive: true })` throws for any disjunct that is disjoint with the existing root. Reproduced on the branch: `number>=0!=0`, `number.integer>=0!=0`, `number<=5!=5`, `number>=5!=5`, `number<0!=5`, `string>=2!=2`, `string>2!=2`, `string<3!=3`, `Date>d'2020-01-02'!=d'2020-01-01'`.
- `ark/schema/roots/root.ts:594` — `constrain` runs `result.throw()` on `Disjoint`, so the union never gets a chance to drop the empty branch.

## Required outcome
- A disjoint disjunct must be treated as `never` and unioned away rather than aborting: `number>=0!=0` ⇒ `number>0`; `number<0!=5` ⇒ `number<0`; `number<=5!=5` ⇒ `number<5`; `string>=2!=2` ⇒ `string>=3`.

## Suggested approach (optional)
- Build each disjunct through a non-throwing intersection (or check disjointness via `extends`) and `rawOr` the survivors; the existing `==` intersection path can stay as-is.

## Open questions for the human (optional)
- The issue discussion flagged "new intersections to consider"; confirm that chained `!=` after a bound should be supported rather than explicitly rejected.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f06ed03, with tests. Thanks!

Comment thread ark/type/parser/shift/tokens.ts Outdated
"<": 1,
">": 1,
"=": 1,
"!": 1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding ! to terminatingChars also terminates unenclosed operand scans, so brand names and scope alias keys containing ! no longer parse: type("string#a!b") previously became a branded string and now throws '!' is not allowed here, because parseBrand stops the name at ! (ark/type/parser/shift/operator/brand.ts:9). If arbitrary brand names are intended to be supported, consider stopping the scan only at a ! that is followed by =.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f06ed03, with tests. Thanks!

When an existing bound rules out one side of !=, drop that side instead
of throwing. For example, nonNegative!=0 now reduces to number>0.

Only end a name at !=, so a lone ! in brand names and aliases, like
string#a!b, parses again.
ssalbdivad added a commit that referenced this pull request Oct 22, 2026
`!=` constrains what `==` does to anything but its limit, so it takes a
number, a string or array's length, or a Date, in the single right-bound
position `==` takes; like `==`, it can't pair with a left bound. Each
form is the negation of what `==` makes:

- `==` on a number or Date is a unit, so `!=` on one is `notEqual`, whose
  rule is a number or a Date. It compiles `data !== 5`, or
  `data.valueOf() !== 957312000000` for a Date. A third kind for Dates,
  like before/after beside min/max, would add a class and its
  registrations for no difference in the algebra, and instanceof tells
  the rules apart. A numeric limit on a Date is read as epoch
  milliseconds, as it is for Date bounds.
- `==` on a length is exactLength, so `!=` on one is `notEqualLength`.

Both are open kinds, like pattern, so `number != 0 & number != 5` holds
both, ordered by value so equal intersections share a key.

The algebra is in arksets alone, beside every other kind's:

- A bound excluding the value makes the != redundant, and one including
  it only at its limit moves past it: `number >= 0 & number != 0` is
  `number > 0`, `string <= 3 & string != 3` is `string <= 2`.
- An exactLength or unit at the value is disjoint from it.
- A divisor the value isn't a multiple of makes it redundant.
- `notEqualLength` 0 reduces to `minLength` 1, since no length is below
  0, so `string != 0` and `string > 0` are one node.
- A union's branches meeting at one value neither includes merge into
  one excluding it: `number < 0 | number > 0` is `number != 0`, and
  `0 < number < 5 | 5 < number < 10` is `number < 10 & > 0 & != 5`.
  Lengths and Dates are integers whose exclusive bounds are normalized
  to inclusive ones, so theirs merge across limits 2 apart, as
  `string <= 2 | string >= 4` is `string != 3`. The merge is skipped
  where the basis allows a value every bound rejects, NaN under
  numberAllowsNaN or an invalid Date under dateAllowsInvalid, since `!=`
  allows it, and in ordered unions.

JSON Schema writes `not: { const: 0 }`, `not: { enum: [...] }` for
several, and `not: { type: "string", minLength: 3, maxLength: 3 }` for a
length, with `anyOf` inside `not` for several; a Date's goes through the
date fallback, which annotates `x-arktype-notEqual`.

English describes the constraint as `other than 0`, so a failure reads
`must be other than 0 (was 0)`, the phrasing arksets uses for a JSON
Schema `not`, so one constraint reads one way however it was written. A
Date's actual is written as a date, a length's as its length. German
writes `muss ungleich 0 sein` (`ungleich dem 3. Mai 2000` for a Date)
and, for lengths, `darf nicht 2 Zeichen lang sein`, since `muss nicht`
means "need not". That brings de to 3,694 bytes gzipped, so localeSize's
budget goes from 3,600 to 3,700: two codes across three operand kinds
cost 95 B, after dropping the writers de's defaults already cover.

The tokenizer is PR #1692's: a lone `!` doesn't end a name, so an alias
or brand like `a!b` parses as before, while `!` directly before `=`
does, at runtime by stepping back over it and at the type level by
checking the token's end only when the rest starts with `=`, which
costs the type benches 1-3.6% (divisor +3.5%, bound-double +3.6%,
intersection-string +2.6%). A wrapper matching both ends at once cost
5-9%, and a single tuple-template match 13-20%.

fast-check filters an intersection's arbitrary by its exclusions, and
the precompiled corpus covers all three operand kinds.

Fixes #925

Co-authored-by: jkugsiya <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
ssalbdivad added a commit that referenced this pull request Oct 22, 2026
…ge replaces

Cite #925 on the != entry and credit @jkugsiya, whose #1692 tokenizer it builds on. Cite #751 inline as the rest of the CHANGELOG does, rather than as Markdown links. The example of a one-value gap now prints the expression as it parses back, 0 < (number != 5) < 10. The note on a union's configured message now names what it still replaces, an error at the object itself, instead of only what it no longer replaces.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

Support for != bound operator

1 participant