Skip to content

Commit

Permalink
fix(type-utils): union types always being marked as readonly (#4419)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens authored Jan 17, 2022
1 parent ef3147c commit 99ab193
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/type-utils/src/isTypeReadonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ function isTypeReadonlyRecurser(
const result = unionTypeParts(type).every(
t =>
seenTypes.has(t) ||
isTypeReadonlyRecurser(checker, t, options, seenTypes),
isTypeReadonlyRecurser(checker, t, options, seenTypes) ===
Readonlyness.Readonly,
);
const readonlyness = result ? Readonlyness.Readonly : Readonlyness.Mutable;
return readonlyness;
Expand Down
27 changes: 27 additions & 0 deletions packages/type-utils/tests/isTypeReadonly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ describe('isTypeReadonly', () => {
);
});
});

describe('Union', () => {
describe('is readonly', () => {
const runTests = runTestIsReadonly;

it.each([
[
'type Test = Readonly<{ foo: string; bar: number; }> & Readonly<{ bar: number; }>;',
],
['type Test = readonly string[] | readonly number[];'],
])('handles a union of 2 fully readonly types', runTests);
});

describe('is not readonly', () => {
const runTests = runTestIsNotReadonly;

it.each([
['type Test = { foo: string; bar: number; } | { bar: number; };'],
[
'type Test = { foo: string; bar: number; } | Readonly<{ bar: number; }>;',
],
[
'type Test = Readonly<{ foo: string; bar: number; }> | { bar: number; };',
],
])('handles a union of non fully readonly types', runTests);
});
});
});

describe('treatMethodsAsReadonly', () => {
Expand Down

0 comments on commit 99ab193

Please sign in to comment.