microsoft / microsoft/TypeScript

Class with 'private [Symbol.iterator]' is iterable outside of the class body

Open
#42,190 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: classes
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.2.0-dev.20210103, seems to be a regression in 3.6, probably introduced by strictly typed generators (cc @rbuckton)

Search Terms: iteration, private

Expected behavior:

Errors like in v3.5:

Type 'C' is not assignable to type 'Iterable<string>'.
  Property '[Symbol.iterator]' is private in type 'C' but not in type 'Iterable<string>'.

Type 'D' is not assignable to type 'Iterable<string>'.
  Types of property '[Symbol.iterator]' are incompatible.
    Type '() => D' is not assignable to type '() => Iterator<string>'.
      Type 'D' is not assignable to type 'Iterator<string>'.
        Property 'next' is private in type 'D' but not in type 'Iterator<string>'.

Actual behavior:

No error on iteration inside and outside of the class body.

Related Issues: #42051 and #42104

Code

class C {
    private [Symbol.iterator]() {
        return this;
    }

    public next() {
        return {value: 'a', done: false};
    }
}

class D {
    public [Symbol.iterator]() {
        return this;
    }

    private next() {
        return {value: 'a', done: false};
    }
}


function fn() {
    for (const _ of new C()) {
    }

    for (const _ of new D()) {}
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Playground reproduction and compare its TypeScript 4.2-dev behavior with the reported 3.5 behavior, using related issues #42051 and #42104 for context. Trace the compiler's handling of private [Symbol.iterator] and private next members; done means the invalid iterations produce the expected assignability errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.