Skip to content

es-get-iterator broken by IIFE now allowing contextual return type  #45559

Closed

Description

Broken by #45007:

interface Iterable<T, TReturn = unknown, TNext = undefined> {
    [Symbol.iterator](): Iterator<T, TReturn, TNext>;
}

type InferIterable<T> = T extends Iterable<infer TYield, infer TReturn, infer TNext>
    ? Iterator<TYield, TReturn, TNext>
    : unknown extends T
        ? Iterator<unknown> | undefined
        : undefined;

declare function getIterator<T>(iterable: T): InferIterable<T>;

var x = getIterator((function*() { yield "foo"; yield "bar"; })());

Expected: x: Iterator<"foo" | "bar", void, unknown>

Actual: x: Iterator<"foo" | "bar", void, any>

I think this is an acceptable break, but I want another round of thinking about it now that we have a concrete example.

Activity

  1. sandersn commented on Aug 24, 2021

    @sandersn
    MemberAuthor

    Daniel Rosenwasser (@DanielRosenwasser) you probably want to know about this too.

  2. sandersn commented on Oct 5, 2021

    @sandersn
    MemberAuthor

    Smaller repro:

    declare function getIterator<T>(iterable: T): void;
    getIterator((function*() { yield "foo"; yield "bar"; })());
  3. sandersn commented on Oct 6, 2021

    @sandersn
    MemberAuthor

    This is an inconsistency in our iterator checking; with no contextual type, the hard-coded TNext default is unknown; with a defective, next-free contextual type, the hard-coded default is any instead.

    We should maybe consider fixing this, but only if it doesn't break anybody, or if anybody thinks the current state is bad enough.

    In any case, I think this change is fine to ship as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Breaking ChangeWould introduce errors in existing code

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions