Skip to content

Commit

Permalink
fix(eslint-plugin): skip seenTypes for unions in isTypeReadonly (#4043)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Goldberg committed Nov 1, 2021
1 parent 80b853d commit 6af7ca7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/eslint-plugin/src/util/isTypeReadonly.ts
Expand Up @@ -185,8 +185,10 @@ function isTypeReadonlyRecurser(

if (isUnionType(type)) {
// all types in the union must be readonly
const result = unionTypeParts(type).every(t =>
isTypeReadonlyRecurser(checker, t, options, seenTypes),
const result = unionTypeParts(type).every(
t =>
seenTypes.has(t) ||
isTypeReadonlyRecurser(checker, t, options, seenTypes),
);
const readonlyness = result ? Readonlyness.Readonly : Readonlyness.Mutable;
return readonlyness;
Expand Down
Expand Up @@ -280,13 +280,23 @@ ruleTester.run('prefer-readonly-parameter-types', rule, {
`, // TSMethodSignature

// https://github.com/typescript-eslint/typescript-eslint/issues/1665
// directly recursive
// directly recursive interface
`
interface Foo {
readonly prop: Foo;
}
function foo(arg: Foo) {}
`,

// https://github.com/typescript-eslint/typescript-eslint/issues/3396
// directly recursive union type
`
type MyType = string | readonly MyType[];
function foo<A extends MyType[]>(a: A): MyType[] {
return [];
}
`,
// indirectly recursive
`
interface Foo {
Expand Down

0 comments on commit 6af7ca7

Please sign in to comment.