Skip to content

Add iterate functions to collection types in vscode.d.ts #151802

@mjbvz

Description

@mjbvz

vscode.d.ts defines a few collection types: DiagnosticCollection, DataTransfer, EnvironmentVariableCollection, TestItemCollection. Currently we only expose a forEach method on each of these to iterate through them. This works but is annoying for a few reasons:

  1. You can't use the with for...of loops

  2. forEach doesn't play nicely with async/await. This is specifically an issue for DataTransfer where you end up writing code such as:

const promises: Thenable<string>[] = [];
dataTransfer.forEach(x => {
	promises.push(x.asString());
});
const result = Promise.all(promises);
  1. forEach uses a closure which messes up TS's type guards:
let thing: string | number = 1;
if (typeof thing === 'number') {
	return;
}
dataTransfer.forEach(x => {
	thing.charAt(0) // 💥  Thing is back to being a `string | number` again
});

Potential fixes

Two potential approaches:

  1. Add .entries() function to these collections. These would behave like new Map().entries()

  2. Make these types iterable directly by defining a [Symbol.iterabor] function on them

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions