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.60 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.59 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.58 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.57 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.56 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.55 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.54 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.53 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.52 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.51 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.50 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.49 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.48 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.47 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.46 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.45 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.44 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.43 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.42 node_modules/type-fest/source/iterable-element.d.ts
immosquare-cleaner-0.1.41 node_modules/type-fest/source/iterable-element.d.ts