diff --git a/readme.md b/readme.md index 0227dd669..e094442a6 100644 --- a/readme.md +++ b/readme.md @@ -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. @@ -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 diff --git a/source/iterable-element.d.ts b/source/iterable-element.d.ts index 57261fabf..0d8766698 100644 --- a/source/iterable-element.d.ts +++ b/source/iterable-element.d.ts @@ -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. @@ -33,7 +33,7 @@ async function * iAmGeneratorAsync() { type MeStringOrBoolean = IterableElement> ``` -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: @@ -44,6 +44,16 @@ import type {IterableElement} from 'type-fest'; type MeString = IterableElement ``` +@example +``` +import type {IterableElement} from 'type-fest'; + +const fruits = new Set(['🍎', '🍌', '🍉'] as const); + +type Fruit = IterableElement; +//=> '🍎' | '🍌' | '🍉' +``` + @category Iterable */ export type IterableElement = diff --git a/test-d/iterable-element.ts b/test-d/iterable-element.ts index 1d7be6b0c..6b5af8732 100644 --- a/test-d/iterable-element.ts +++ b/test-d/iterable-element.ts @@ -1,4 +1,4 @@ -import {expectType} from 'tsd'; +import {expectAssignable, expectType} from 'tsd'; import type {IterableElement} from '../index'; declare const iterableElement: IterableElement>; @@ -16,3 +16,25 @@ async function * secretGeneratorAsync() { yield true; yield new Date(); } + +const fruits = new Set(['🍎', '🍌', '🍉'] as const); + +type Fruit = IterableElement; + +expectAssignable('🍎'); +expectAssignable('🍌'); +expectAssignable('🍉'); + +type VegetableSet = Set<'🥦' | '🥕' | '🌶'>; +type Vegetable = IterableElement; + +expectAssignable('🥦'); +expectAssignable('🥕'); +expectAssignable('🌶'); + +type UserRolesSet = ReadonlySet<'regular' | 'contributor' | 'maintainer'>; +type UserRole = IterableElement; + +expectAssignable('regular'); +expectAssignable('contributor'); +expectAssignable('maintainer');