Skip to content

Implicit Symbol.iterator call does not check additional argumentsΒ #57130

@rotu

Description

@rotu

πŸ”Ž Search Terms

Symbol.iterator, for-of, iterable protocol

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Iterator

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAtgTwJJQKYCcCGAjANqmAXhgG8AoGSmAKgG0BlBObEXAOgEs0soR0BdABSoAHlCwBBdAHMAXDDABXZhgCUpClS0IOqXABNSMTPNHjMU6TAC+myrdtkA9E+NgYGdHxgALTAAd-VEhfDFQ2GABxEBB9NjIAM29BUEhYERgQBPhkbhx8BiYWdi4MTF4BQVVVci0RNkwyRxcFEA90L3RQ9HCYACFMQ1kAWkFE5NToGAysnJQyvFQau2mGprIgA

πŸ’» Code

const myIterable = {
    *[Symbol.iterator](extraArg: number) {
        yield { a: extraArg }
    }
}

// an error happens here. Good.
for (const x of myIterable[Symbol.iterator]()){
    x.a
}

// no error here. Bad :-(
for (const x of myIterable){
    x.a
}

πŸ™ Actual behavior

When using a for-of loop, there is no type error.

πŸ™‚ Expected behavior

I expect an error to be reported, as if explicitly calling the [Symbol.iterator] method.

Additional information about the issue

Related to #38388, which points out that resolution does not infer this generic type parameter.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions