Skip to content

Functions allowed in weird cases where an indexer signature is expected, where functions don't have one. #42708

@benlesh

Description

@benlesh

Bug Report

Looking for a workaround or an explanation, really. But it seems like a bug?

🔎 Search Terms

  • arraylike
  • index
  • function

🕗 Version & Regression Information

This is the behavior in every version I've tried.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface ArrayLike<T> {
    readonly length: number;
    readonly [n: number]: T;
}

function testB<T>(arg: ArrayLike<T>) { }

// GOOD: Errors here as expected. "index signature is missing".
testB(() => {});

function testA(arg: ArrayLike<any>) {}

// BAD: No errors here? The index signature is still missing, though.
testA(() => {});

🙁 Actual behavior

testA allows a developer to pass a function where we didn't intend them to be able to pass a function.

In some (much more complicated) cases in RxJS this becomes an unintuitive footgun.

🙂 Expected behavior

I'd expect that testA would error with the same error that testB errors with. Functions don't have indexers in their signatures.

Related to:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions