microsoft / microsoft/TypeScript
Class with 'private [Symbol.iterator]' is iterable outside of the class body
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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