Skip to content

Commit

Permalink
fix(type-utils): isTypeReadonly now handles conditional types
Browse files Browse the repository at this point in the history
fix #4420
  • Loading branch information
RebeccaStevens committed Jan 10, 2022
1 parent f6e5ffd commit bb6de67
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/type-utils/src/isTypeReadonly.ts
@@ -1,5 +1,6 @@
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import {
isConditionalType,
isObjectType,
isUnionType,
isUnionOrIntersectionType,
Expand Down Expand Up @@ -198,6 +199,20 @@ function isTypeReadonlyRecurser(
return readonlyness;
}

if (isConditionalType(type)) {
const result = [type.root.node.trueType, type.root.node.falseType]
.map(checker.getTypeFromTypeNode)
.every(
t =>
seenTypes.has(t) ||
isTypeReadonlyRecurser(checker, t, options, seenTypes) ===
Readonlyness.Readonly,
);

const readonlyness = result ? Readonlyness.Readonly : Readonlyness.Mutable;
return readonlyness;
}

// all non-object, non-intersection types are readonly.
// this should only be primitive types
if (!isObjectType(type) && !isUnionOrIntersectionType(type)) {
Expand Down

0 comments on commit bb6de67

Please sign in to comment.