Skip to content

Commit

Permalink
fix(type-utils): intersection types involving readonly arrays are now…
Browse files Browse the repository at this point in the history
… handled in most cases

fix #4428
  • Loading branch information
RebeccaStevens committed Jan 10, 2022
1 parent 011647f commit ba0f0ac
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions packages/type-utils/src/isTypeReadonly.ts
Expand Up @@ -2,10 +2,10 @@ import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import {
isObjectType,
isUnionType,
isUnionOrIntersectionType,
unionTypeParts,
isPropertyReadonlyInType,
isSymbolFlagSet,
isIntersectionType,
} from 'tsutils';
import * as ts from 'typescript';
import { getTypeOfPropertyOfType } from './propertyTypes';
Expand Down Expand Up @@ -198,9 +198,35 @@ function isTypeReadonlyRecurser(
return readonlyness;
}

// all non-object, non-intersection types are readonly.
if (isIntersectionType(type)) {
// Special case for handling arrays/tuples (as readonly arrays/tuples always have mutable methods).
if (
type.types.some(t => checker.isArrayType(t) || checker.isTupleType(t))
) {
const allReadonlyParts = type.types.every(
t =>
seenTypes.has(t) ||
isTypeReadonlyRecurser(checker, t, options, seenTypes) ===
Readonlyness.Readonly,
);
return allReadonlyParts ? Readonlyness.Readonly : Readonlyness.Mutable;
}

// Normal case.
const isReadonlyObject = isTypeReadonlyObject(
checker,
type,
options,
seenTypes,
);
if (isReadonlyObject !== Readonlyness.UnknownType) {
return isReadonlyObject;
}
}

// all non-object are readonly.
// this should only be primitive types
if (!isObjectType(type) && !isUnionOrIntersectionType(type)) {
if (!isObjectType(type)) {
return Readonlyness.Readonly;
}

Expand Down

0 comments on commit ba0f0ac

Please sign in to comment.