Skip to content

Commit

Permalink
fix(type-utils): check IndexSignature internals when checking isTypeR…
Browse files Browse the repository at this point in the history
…eadonly

fix #3714
  • Loading branch information
RebeccaStevens committed Jan 10, 2022
1 parent 8906f67 commit 12aea42
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions packages/type-utils/src/isTypeReadonly.ts
Expand Up @@ -107,9 +107,18 @@ function isTypeReadonlyObject(
function checkIndexSignature(kind: ts.IndexKind): Readonlyness {
const indexInfo = checker.getIndexInfoOfType(type, kind);
if (indexInfo) {
return indexInfo.isReadonly
? Readonlyness.Readonly
: Readonlyness.Mutable;
if (!indexInfo.isReadonly) {
return Readonlyness.Mutable;
}

return (
isTypeReadonlyRecurser(
checker,
indexInfo.keyType,
options,
seenTypes,
) && isTypeReadonlyRecurser(checker, indexInfo.type, options, seenTypes)
);
}

return Readonlyness.UnknownType;
Expand All @@ -119,20 +128,37 @@ function isTypeReadonlyObject(
if (properties.length) {
// ensure the properties are marked as readonly
for (const property of properties) {
if (
!(
isPropertyReadonlyInType(type, property.getEscapedName(), checker) ||
(options.treatMethodsAsReadonly &&
property.valueDeclaration !== undefined &&
hasSymbol(property.valueDeclaration) &&
isSymbolFlagSet(
property.valueDeclaration.symbol,
ts.SymbolFlags.Method,
))
)
) {
return Readonlyness.Mutable;
if (options.treatMethodsAsReadonly) {
if (
property.valueDeclaration !== undefined &&
hasSymbol(property.valueDeclaration) &&
isSymbolFlagSet(
property.valueDeclaration.symbol,
ts.SymbolFlags.Method,
)
) {
continue;
}

const declarations = property.getDeclarations();
const lastDeclaration =
declarations !== undefined && declarations.length > 0
? declarations[declarations.length - 1]
: undefined;
if (
lastDeclaration !== undefined &&
hasSymbol(lastDeclaration) &&
isSymbolFlagSet(lastDeclaration.symbol, ts.SymbolFlags.Method)
) {
continue;
}
}

if (isPropertyReadonlyInType(type, property.getEscapedName(), checker)) {
continue;
}

return Readonlyness.Mutable;
}

// all properties were readonly
Expand Down

0 comments on commit 12aea42

Please sign in to comment.