Sha256: 7086cca41a87b3bf52c6abfc37cda0a0ec86bb7e8e5ef166b07976abec73fa5e

Contents?: true

Size: 1.42 KB

Versions: 33

Compression:

Stored size: 1.42 KB

Contents

/**
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 `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. For example, `Array`, `Set`, `Map`, `stream.Readable`, etc.

An example with an array of strings:

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

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

@category Iterable
*/
export type IterableElement<TargetIterable> =
	TargetIterable extends Iterable<infer ElementType> ?
	ElementType :
	TargetIterable extends AsyncIterable<infer ElementType> ?
	ElementType :
	never;

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.40 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.39 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.38 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.32 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.31 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.30 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.29 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.28 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.27 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.26 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.25 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.24 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.23 node_modules/boxen/node_modules/type-fest/source/iterable-element.d.ts