Skip to content

TS 5.6 Beta BuiltinIteratorReturn: can this be the default type argument of BuiltinIterator's TReturn? #59444

Closed
@uhyo

Description

@uhyo

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

#58243 introduced the BuiltinIteratorReturn type and used it everywhere in type definition.

Then #58222 introduced the BuiltinIterator type and replaced some IterableIterator usage with BuiltinIterator.

I wonder if BuiltinIteratorReturn could be set to the default type argument of the TReturn type parameter of BuiltinIterator, e.g.:

// lib.es2015.iterable.d.ts
interface BuiltinIterator<T, TReturn = BuiltinIteratorReturn, TNext = any> extends Iterator<T, TReturn, TNext> {
    [Symbol.iterator](): BuiltinIterator<T, TReturn, TNext>;
}

#58463 mentions that BuiltinIteratorReturn cannot be used for IterableIterator because IterableIterator is a general purpose type not only for built-in iterators. However, this doesn't apply to BuiltinIterator. I think we can safely use BuiltinIteratorReturn specifically for BuiltinIterator's default type argument.

The downside of current approach (specify BuiltinIteratorReturn everywhere) is that it is too easy to disable the benefit of the new compiler option by manually using the BuiltinIterator type:

const nums = new Set([1, 2, 3, 4, 5]);

// oops! This is BuiltinIterator<number, any, any> regardless of compiler option
const values: BuiltinIterator<number> = nums.values();

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions