Repository navigation
in and instanceof on a union of interfaces - #737
Merged
Merged
Conversation
`"swim" in a` for `a: Fish | Bird` was "not supported", and `a instanceof Fish` was always false (the types compared, `Fish | Bird` against `Fish`). Every interface's union tag reads "interface", so nothing at run time said which interface the union held. Which members have the key, or are the interface, is decided when compiling: every one or none folds to a constant. Otherwise the union's tag is asked: an interface's descriptor now has an id of its name in TYPE_DESCR_LAYOUT, and ts.IsTypeTagOf compares a tag's with a type's. `"k" in a` narrows `a` to the members with the key (and the `else` to the others), as `instanceof` already narrowed. Test: 00in_interface_union (compile and JIT; gc, rc, none, own). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
added a commit
that referenced
this pull request
Oct 29, 2026
…s; design notes (#738) * `in` and `instanceof` on a union of interfaces `"swim" in a` for `a: Fish | Bird` was "not supported", and `a instanceof Fish` was always false (the types compared, `Fish | Bird` against `Fish`). Every interface's union tag reads "interface", so nothing at run time said which interface the union held. Which members have the key, or are the interface, is decided when compiling: every one or none folds to a constant. Otherwise the union's tag is asked: an interface's descriptor now has an id of its name in TYPE_DESCR_LAYOUT, and ts.IsTypeTagOf compares a tag's with a type's. `"k" in a` narrows `a` to the members with the key (and the `else` to the others), as `instanceof` already narrowed. Test: 00in_interface_union (compile and JIT; gc, rc, none, own). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * An object literal or class object given to a union of interfaces `move({ swim: () => "s" })` for `move(a: Fish | Bird)` failed with "'ts.Cast' op type [...] can't be stored in [...]", and so did a class object. castToTaggedUnionType now casts such a value to the first interface member whose every required field and method it has (interfaceMemberFitting, by name, as `"k" in v` asks), and the union's tag says which (`"swim" in a`, #737). Test: 00interface_union_literal (compile and JIT; gc, rc, none, own). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fewer-parameter callbacks: generic inference and a thunk's owned result Two bugs in the fewer-parameters thunk (#733, #735), found by giving the default library's array callbacks an index parameter: - A closure given for a generic callback `(v: T, i: number) => void` had its captures paired with `T` (`string | Reference<{...}>`): its type is its function bound over its captures. Inference now drops the bound first parameter, of the closure's type and of the arrow type rebuilt. - Under -mm=own a heap result the thunk hands on (a string a `map` callback builds) was "a second reference": the thunk's call is now marked owned and consumed by its return, as `return f(x)` is. Test: 00closure_fewer_parameters (a generic callback, a string result). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Design notes: generic interface methods, classes as values - docs/generic-interface-methods-design.md: generic methods in interfaces (rejected today) and in class hierarchies (not virtual: an override is never reached through a base reference, silently). Options: erased `any` slots, a closed-world table, dictionary passing; recommends an error for overridden generic class methods now, erased slots later. - docs/classes-as-values-design.md: `typeof A` values (ts.ClassRef has no value lowering) and local classes that capture (members lifted with an extra parameter nobody passes). Recommends the vtable as the class value with a `.constructor` slot, and the captures in a hidden `.captured` field of the instance. - docs/tuple-object-shape-ambiguity-design.md: re-checked, still open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
inandinstanceofnow work on a union of interfaces.For
a: Fish | Bird,"swim" in awas "not supported", anda instanceof Fishwas alwaysfalse: it compared the typeFish | BirdwithFish. Both members are interfaces, and an interface's union tag always reads "interface", so nothing at run time said which interface the union held.instanceofinterface. If every member qualifies, the result is the constanttrue; if none does,false.TYPE_DESCR_LAYOUT, the slot tuples use for their layout id (any to object type checks fields; own: literal with capturing arrows given to an object type (spec 66) #734). The new opts.IsTypeTagOfcompares a tag's id with an interface's. The id comes from the name, so it matches across modules.if ("k" in a)narrowsato the members that have the key, and theelsebranch to the others, asinstanceofalready did.Test:
00in_interface_union(compile and JIT; gc, rc, none, own). It covers two- and three-member unions,elsenarrowing,instanceof, and keys that every member or no member has.Validation: ctest 6327/6327. DefaultLib release JIT: gc 181/181, rc and none 180 (1 skipped), own 170 (11 skipped).
🤖 Generated with Claude Code