Skip to content

Incorrect type error when assigning to an index in a {}[]-typed field where the index is any-typed and field is defined in a base class in a JS file #2655

@peetklecha

Description

@peetklecha

Steps to reproduce

// dep.js

export class BaseClass {
    constructor() {
        this.map = [];
        this.map.push(function(){});
    }
}

// main.ts

import { BaseClass } from "./dep.js";

declare const key: any;

class Item extends BaseClass {
    constructor() {
        super();
        this.map[key] = {};
//      ~~~~~~~~~~~~~
// Type '{}' is not assignable to type '{}'. Two different types with this name exist, but they are unrelated.
    }
}

Behavior with typescript@5.9

No error is raised.

Behavior with tsgo

The line this.map[key] = {} raises error 2719; or if any other value is used (e.g., this.map[key] = 7) error 2322. I have noticed other cases where tsgo seems not quite do quite such aggressive type inference on arrays defined in JS files but in this case it does seemingly still identify map as having type {}[].

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions