Skip to content

A record literal with a run-time field cannot be assigned to a class type (invalid cast) #487

Closed

Description

@ASDAlexander77

A record literal with a field known only at run time cannot be assigned to a class type.

class P {
    x: number;
    items: number[];
}

function main() {
    let i = 7;
    const p: P = { x: 1, items: [i, 2] };
    print(p.items.length);
}
error: invalid cast from {x:s32, items:s32[]} to P

Expected: 2.

With every field constant ({ x: 1, items: [1, 2] }), the record is a constant tuple and the cast works (castTupleLikeVariants: constant tuple to class goes through castTupleToClass). A record with a run-time field is a ts.CreateTuple/slot value of tuple type, and the tuple-to-class branch reports "invalid cast" instead of building the instance the same way.

Same on main (77d9a6a) and with #484.

Found while testing #484 (#479).

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions