Skip to content

instanceof with a primitive value on the right is an error - #585

Merged
ASDAlexander77 merged 1 commit into
mainfrom
instanceof-primitive-error
Oct 17, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
instanceof-primitive-error

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

5 instanceof 3 compiled with no diagnostic. With no class on the right, mlirGenInstanceOfLogic fell through to comparing the two types, giving a constant.

Fix: a number, string, boolean or bigint value on the right of instanceof is an error at the right operand.

Repro (diagnostic-location/instanceof_primitive.ts):

type str = TypeOf<"">;

function main() {
    print("asd" instanceof str);
    print(5 instanceof 3);   // 8:24: error: The right-hand side of an 'instanceof' expression must be a class, not '3'
}

Kept on purpose: a type on the right ("asd" instanceof str, tests/00instanceof.ts). It is a ts.TypeRef, and the test still compares the types. Line 7 of the test must stay error-free.

Not changed: a primitive on the left (5 instanceof C). TypeScript rejects it, but in a generic specialised for a number the left is a number legitimately, so it needs the unspecialised type to check.

ctest -C Release -j 8 --timeout 300: 100% passed, 0 failed (4092 tests on this branch).

🤖 Generated with Claude Code

`5 instanceof 3` compiled with no diagnostic: with no class on the
right, the test compared the two types and was a constant. TypeScript
rejects a right-hand side that is no class or function; tslang now
rejects a number, string, boolean or bigint value there, at the right
operand. A type on the right (`type str = TypeOf<"">`, as in
tests/00instanceof.ts) stays an extension that compares the types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 7d26030 into main Oct 17, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the instanceof-primitive-error branch October 17, 2026 14:12
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.

1 participant