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 851b9be
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/type-utils/src/isTypeReadonly.ts
Expand Up @@ -6,6 +6,7 @@ import {
unionTypeParts,
isPropertyReadonlyInType,
isSymbolFlagSet,
isIntersectionType,
} from 'tsutils';
import * as ts from 'typescript';
import { getTypeOfPropertyOfType } from './propertyTypes';
Expand Down Expand Up @@ -198,9 +199,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 851b9be

Please sign in to comment.