Sha256: 6addbb18f70100a2de900bace1c800b8d760421cdd33c1d69ee290b71e28003d

Contents?: true

Size: 1.58 KB

Versions: 28

Compression:

Stored size: 1.58 KB

Contents

/**
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
*/
export type IterableElement<TargetIterable> =
	TargetIterable extends Iterable<infer ElementType> ?
		ElementType :
		TargetIterable extends AsyncIterable<infer ElementType> ?
			ElementType :
			never;

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/type-fest/source/iterable-element.d.ts