Skip to content

Commit

Permalink
fix(readonly): handle readonly arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
skovy authored Oct 29, 2019
1 parent 595d7db commit d7b68ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,22 @@ describe("array", () => {
const records = array(record, 2);
expect(records()).toHaveLength(2);
});

test("readonly arrays", () => {
type Bar = {
index: number;
};

type Foo = {
readonly bars: ReadonlyArray<Bar>;
};

const barFactory = define<Bar>({ index: sequence });

const fooFactory = define<Foo>({
bars: array(barFactory, 2)
});

expect(fooFactory().bars).toHaveLength(2);
});
});
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ type Diff<T, U> = T extends U ? never : T;
// into `{ b: number; }`
type DiffProperties<T, U> = Pick<T, Diff<Keys<T>, Keys<U>>>;

export type ArrayElement<ArrayType> = ArrayType extends (infer ElementType)[]
// Given an array, infer the type of it's elements
export type ArrayElement<ArrayType> = ArrayType extends ReadonlyArray<
infer ElementType
>
? ElementType
: never;

Expand Down

0 comments on commit d7b68ac

Please sign in to comment.