Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(type-utils): check IndexSignature internals when checking isTypeReadonly#4417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 9 commits intotypescript-eslint:mainfromRebeccaStevens:issue-3714
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
9 commits
Select commitHold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletionspackages/type-utils/src/isTypeReadonly.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -107,9 +107,16 @@ 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.type,
options,
seenTypes,
);
}

return Readonlyness.UnknownType;
Expand All@@ -119,20 +126,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
28 changes: 28 additions & 0 deletionspackages/type-utils/tests/isTypeReadonly.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,6 +109,34 @@ describe('isTypeReadonly', () => {
])('handles non fully readonly sets and maps', runTests);
});
});

describe('IndexSignature', () => {
describe('is readonly', () => {
const runTests = runTestIsReadonly;

it.each([
['type Test = { readonly [key: string]: string };'],
[
'type Test = { readonly [key: string]: { readonly foo: readonly string[]; }; };',
],
])(
'handles readonly PropertySignature inside a readonly IndexSignature',
runTests,
);
});

describe('is not readonly', () => {
const runTests = runTestIsNotReadonly;

it.each([
['type Test = { [key: string]: string };'],
['type Test = { readonly [key: string]: { foo: string[]; }; };'],
])(
'handles mutable PropertySignature inside a readonly IndexSignature',
runTests,
);
});
});
});

describe('treatMethodsAsReadonly', () => {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp