Skip to main content
Module

x/scaffold/src/deps/types.ts>IterableElement

scaffold your next project with style and 💗
Latest
type alias IterableElement
import { type IterableElement } from "https://deno.land/x/scaffold@0.3.0/src/deps/types.ts";

Get the element type of an Iterable/AsyncIterable. For example, an array or a generator.

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 Iterables and AsyncIterables, so it can be use with synchronous and asynchronous generators.

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

Examples

Example 1

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

functioniAmGenerator() {
	yield 1;
	yield 2;
}

type MeNumber = IterableElement<ReturnType<typeof iAmGenerator>>

And here is an example with an async generator:

Example 2

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

async functioniAmGeneratorAsync() {
	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. For example, Array, Set, Map, stream.Readable, etc.

An example with an array of strings:

Example 3

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

type MeString = IterableElement<string[]>

Type Parameters

TargetIterable
definition: TargetIterable extends Iterable<infer ElementType> ? ElementType : TargetIterable extends AsyncIterable<infer ElementType> ? ElementType : never