Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,27 @@ ruleTester.run('prefer-readonly-parameter-types', rule, {
},
],
},
{
name: 'circular readonly types (Bug: #5875)',
code: `
interface Obj1 {
readonly [K: string]: Obj2;
}

interface Obj2 {
readonly [K: string]: Obj1;
}

function foo(event: Obj1): void {}
`,
options: [
{
checkParameterProperties: true,
ignoreInferredTypes: false,
...readonlynessOptionsDefaults,
},
],
},
],
invalid: [
// arrays
Expand Down
2 changes: 1 addition & 1 deletion packages/type-utils/src/isTypeReadonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function isTypeReadonlyObject(
return Readonlyness.Mutable;
}

if (indexInfo.type === type) {
if (indexInfo.type === type || seenTypes.has(indexInfo.type)) {
return Readonlyness.Readonly;
}

Expand Down
22 changes: 21 additions & 1 deletion packages/type-utils/tests/isTypeReadonly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ describe('isTypeReadonly', () => {

it('handles circular readonly PropertySignature inside a readonly IndexSignature', () =>
runTests('interface Test { readonly [key: string]: Test };'));

it('handles circular readonly PropertySignature inside interdependent objects', () =>
runTests(
'interface Test1 { readonly [key: string]: Test } interface Test { readonly [key: string]: Test1 }',
));
});

describe('is not readonly', () => {
Expand All @@ -156,8 +161,23 @@ describe('isTypeReadonly', () => {
describe('is not readonly circular', () => {
const runTests = runTestIsNotReadonly;

it('handles circular mutable PropertySignature inside a readonly IndexSignature', () =>
it('handles circular mutable PropertySignature', () =>
runTests('interface Test { [key: string]: Test };'));

it.each([
[
'interface Test1 { [key: string]: Test } interface Test { readonly [key: string]: Test1 }',
],
[
'interface Test1 { readonly [key: string]: Test } interface Test { [key: string]: Test1 }',
],
[
'interface Test1 { [key: string]: Test } interface Test { [key: string]: Test1 }',
],
])(
'handles circular mutable PropertySignature inside interdependent objects',
runTests,
);
});
});

Expand Down