Skip to main content

prefer-type-fest-iterable-element

Require TypeFest IterableElement<T> over imported aliases like SetElement, SetEntry, and SetValues.

Targeted pattern scopeโ€‹

This rule focuses on imported legacy iterable helper aliases that TypeFest now models via IterableElement.

  • Type references that resolve to imported SetElement aliases.
  • Type references that resolve to imported SetEntry aliases.
  • Type references that resolve to imported SetValues aliases.

Unrelated type references and aliases from non-targeted imports are intentionally left untouched.

What this rule reportsโ€‹

This rule reports imported iterable helper aliases that should be consolidated to IterableElement.

  • Type references that resolve to imported SetElement aliases.
  • Type references that resolve to imported SetEntry aliases.
  • Type references that resolve to imported SetValues aliases.

Why this rule existsโ€‹

IterableElement is the canonical TypeFest utility for extracting element types from iterable collections. Consolidating on one name makes collection type extraction patterns easier to audit and maintain.

โŒ Incorrectโ€‹

import type { SetElement } from "type-aliases";

type Value = SetElement<Set<string>>;

โœ… Correctโ€‹

import type { IterableElement } from "type-fest";

type Value = IterableElement<Set<string>>;

Behavior and migration notesโ€‹

  • This rule targets imported alias names that duplicate IterableElement semantics (SetElement, SetEntry, SetValues).
  • Standardize on IterableElement<T> for sync and async iterable extraction patterns.
  • Keep legacy alias names only when external libraries expose them as a required public API.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

import type { SetElement } from "type-aliases";

type Item = SetElement<Set<number>>;

โœ… Correct โ€” Additional exampleโ€‹

import type { IterableElement } from "type-fest";

type Item = IterableElement<Set<number>>;

โœ… Correct โ€” Repository-wide usageโ€‹

type StreamChunk = IterableElement<AsyncIterable<string>>;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-type-fest-iterable-element": "error",
},
},
];

When not to use itโ€‹

Disable this rule if compatibility requires preserving external alias names.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/iterable-element.d.ts

/**
Get the element type of an `Iterable`/`AsyncIterable`. For example, `Array`, `Set`, `Map`, generator, stream, etc.

This can be useful, for example, if you want to get the type that is yielded in a generator function. Often the return type of those functions are not specified.

This type works with both `Iterable`s and `AsyncIterable`s, so it can be use with synchronous and asynchronous generators.

Here is an example of `IterableElement` in action with a generator function:

@example
```
import type {IterableElement} from 'type-fest';

function * iAmGenerator() {
yield 1;
yield 2;
}

type MeNumber = IterableElement<ReturnType<typeof iAmGenerator>>;
```

And here is an example with an async generator:

@example
```
import type {IterableElement} from 'type-fest';

async function * iAmGeneratorAsync() {
yield 'hi';
yield true;
}

type MeStringOrBoolean = IterableElement<ReturnType<typeof iAmGeneratorAsync>>;
```

Many types in JavaScript/TypeScript are iterables. This type works on all types that implement those interfaces.

An example with an array of strings:

@example
```
import type {IterableElement} from 'type-fest';

type MeString = IterableElement<string[]>;
```

@example
```
import type {IterableElement} from 'type-fest';

const fruits = new Set(['๐ŸŽ', '๐ŸŒ', '๐Ÿ‰'] as const);

type Fruit = IterableElement<typeof fruits>;
//=> '๐ŸŽ' | '๐ŸŒ' | '๐Ÿ‰'
```

@category Iterable
*/

Rule catalog ID: R042

Further readingโ€‹

Adoption resourcesโ€‹