Skip to content

Commit 11c99b4

Browse files
Cole Chamberlainleebyron
Cole Chamberlain
authored andcommitted
Add RecordOf<TProps> type alias for TypeScript (immutable-js#1578)
* Add `RecordOf<TProps>` type alias for TypeScript The `Record.Factory` API is great but the current definitions lack a simple way to reference instances of a record (created from a record factory). This adds a `RecordOf<TProps>` which can be used to create interfaces containing records: ```ts import { Record, RecordOf } from "immutable"; interface Foo { flag: boolean; message: string; } const FooRecord = Record<Foo>({ flag: false, message: "DEFAULT" }); interface Bar { foo: RecordOf<Foo>; } const bar: Bar = { foo: new FooRecord({ flag: true }) } ``` Currently I've been creating this type in every library so that I can refer to instances and I think Flow has support for something similar. * Made the record instance `ReadOnly` * Fixed typo in `Readonly` and ran prettier
1 parent 43835aa commit 11c99b4

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

type-definitions/Immutable.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,15 @@ declare module Immutable {
25592559
[Symbol.iterator](): IterableIterator<[keyof TProps, TProps[keyof TProps]]>;
25602560
}
25612561

2562+
/**
2563+
* RecordOf<T> is used in TypeScript to define interfaces expecting an
2564+
* instance of record with type T.
2565+
*
2566+
* This is equivalent to an instance of a record created by a Record Factory.
2567+
*/
2568+
export type RecordOf<TProps extends Object> = Record<TProps> &
2569+
Readonly<TProps>;
2570+
25622571
/**
25632572
* `Seq` describes a lazy operation, allowing them to efficiently chain
25642573
* use of all the higher-order collection methods (such as `map` and `filter`)

0 commit comments

Comments
 (0)