Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(type-utils): union types always being marked as readonly #4419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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