Skip to content

Commit

Permalink
IterableElement: Improve docs and add tests for using it with Set (
Browse files Browse the repository at this point in the history
…#894)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
xeho91 and sindresorhus authored Jun 22, 2024
1 parent e75dbe1 commit 0e46755
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Click the type names for complete docs.
- [`LiteralToPrimitive`](source/literal-to-primitive.d.ts) - Convert a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) to the [primitive type](source/primitive.d.ts) it belongs to.
- [`LiteralToPrimitiveDeep`](source/literal-to-primitive-deep.d.ts) - Like `LiteralToPrimitive` except it converts literal types inside an object or array deeply.
- [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, `Array`, `Set`, `Map`, generator, stream, etc.
- [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
- [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
- [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
Expand Down Expand Up @@ -348,6 +348,9 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
- `AllKeys` - See [`KeysOfUnion`](source/keys-of-union.d.ts)
- `Branded` - See [`Tagged`](source/opaque.d.ts)
- `Opaque` - See [`Tagged`](source/opaque.d.ts)
- `SetElement` - See [`IterableElement`](source/iterable-element.d.ts)
- `SetEntry` - See [`IterableElement`](source/iterable-element.d.ts)
- `SetValues` - See [`IterableElement`](source/iterable-element.d.ts)

## Tips

Expand Down
14 changes: 12 additions & 2 deletions source/iterable-element.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
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.
Expand Down Expand Up @@ -33,7 +33,7 @@ async function * iAmGeneratorAsync() {
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.
Many types in JavaScript/TypeScript are iterables. This type works on all types that implement those interfaces.
An example with an array of strings:
Expand All @@ -44,6 +44,16 @@ 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> =
Expand Down
24 changes: 23 additions & 1 deletion test-d/iterable-element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import {expectAssignable, expectType} from 'tsd';
import type {IterableElement} from '../index';

declare const iterableElement: IterableElement<ReturnType<typeof secretGenerator>>;
Expand All @@ -16,3 +16,25 @@ async function * secretGeneratorAsync() {
yield true;
yield new Date();
}

const fruits = new Set(['🍎', '🍌', '🍉'] as const);

type Fruit = IterableElement<typeof fruits>;

expectAssignable<Fruit>('🍎');
expectAssignable<Fruit>('🍌');
expectAssignable<Fruit>('🍉');

type VegetableSet = Set<'🥦' | '🥕' | '🌶'>;
type Vegetable = IterableElement<VegetableSet>;

expectAssignable<Vegetable>('🥦');
expectAssignable<Vegetable>('🥕');
expectAssignable<Vegetable>('🌶');

type UserRolesSet = ReadonlySet<'regular' | 'contributor' | 'maintainer'>;
type UserRole = IterableElement<UserRolesSet>;

expectAssignable<UserRole>('regular');
expectAssignable<UserRole>('contributor');
expectAssignable<UserRole>('maintainer');

0 comments on commit 0e46755

Please sign in to comment.