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): intersection types involving readonly arrays are now handled in most cases#4429

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 23 commits intotypescript-eslint:mainfromRebeccaStevens:issue-4428
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
974858a
fix(type-utils): make isTypeReadonly's options param optional
RebeccaStevensJan 9, 2022
bb209f4
test(type-utils): add basic tests for isTypeReadonly
RebeccaStevensJan 9, 2022
468ac17
test: add union tests for isTypeReadonly
RebeccaStevensJan 9, 2022
dfd5a24
fix(type-utils): union types always being marked as readonly
RebeccaStevensJan 9, 2022
1983d52
test(type-utils): add conditional type tests to isTypeReadonly
RebeccaStevensJan 9, 2022
2b0e454
fix(type-utils): isTypeReadonly now handles conditional types
RebeccaStevensJan 9, 2022
011647f
test(type-utils): add intersections tests for isTypeReadonly
RebeccaStevensJan 10, 2022
ba0f0ac
fix(type-utils): intersection types involving readonly arrays are now…
RebeccaStevensJan 10, 2022
cdb7aff
Merge branch 'main' into issue-4428
Jan 11, 2022
3a3832b
Merge branch 'main' into issue-4420
RebeccaStevensJan 11, 2022
f2a8cf3
Merge branch 'main' into issue-4418
RebeccaStevensJan 11, 2022
491c7bb
Merge branch 'main' into issue-4428
RebeccaStevensJan 12, 2022
669be93
Merge branch 'main' into issue-4420
RebeccaStevensJan 17, 2022
bce11f3
Merge branch 'main' into issue-4418
RebeccaStevensJan 17, 2022
18e4fb9
Merge branch 'main' into issue-4428
RebeccaStevensJan 17, 2022
dc21aa0
Merge branch 'main' into issue-4428
RebeccaStevensJan 17, 2022
5b9a818
Merge branch 'main' into issue-4420
RebeccaStevensJan 17, 2022
7b36d90
Merge branch 'main' into issue-4418
RebeccaStevensJan 17, 2022
727d999
Merge branch 'main' into issue-4418
RebeccaStevensJan 17, 2022
b3122ef
Merge branch 'issue-4418' into issue-4420
RebeccaStevensJan 17, 2022
16e6128
Merge branch 'issue-4420' into issue-4428
RebeccaStevensJan 17, 2022
dd76efd
Merge branch 'main' into issue-4428
bradzacherJan 17, 2022
bd475fb
Update isTypeReadonly.test.ts
bradzacherJan 17, 2022
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
30 changes: 28 additions & 2 deletionspackages/type-utils/src/isTypeReadonly.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,10 +3,10 @@ import {
isConditionalType,
isObjectType,
isUnionType,
isUnionOrIntersectionType,
unionTypeParts,
isPropertyReadonlyInType,
isSymbolFlagSet,
isIntersectionType,
} from 'tsutils';
import * as ts from 'typescript';
import { getTypeOfPropertyOfType } from './propertyTypes';
Expand DownExpand Up@@ -224,6 +224,32 @@ function isTypeReadonlyRecurser(
return readonlyness;
}

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;
}
}

if (isConditionalType(type)) {
const result = [type.root.node.trueType, type.root.node.falseType]
.map(checker.getTypeFromTypeNode)
Expand All@@ -240,7 +266,7 @@ function isTypeReadonlyRecurser(

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

Expand Down
44 changes: 44 additions & 0 deletionspackages/type-utils/tests/isTypeReadonly.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,6 +165,50 @@ describe('isTypeReadonly', () => {
});
});

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

it.each([
[
'type Test = Readonly<{ foo: string; bar: number; }> & Readonly<{ bar: number; }>;',
],
])('handles an intersection of 2 fully readonly types', runTests);

it.each([
[
'type Test = Readonly<{ foo: string; bar: number; }> & { foo: string; };',
],
])(
'handles an intersection of a fully readonly type with a mutable subtype',
runTests,
);

// Array - special case.
// Note: Methods are mutable but arrays are treated special; hence no failure.
it.each([
['type Test = ReadonlyArray<string> & Readonly<{ foo: string; }>;'],
[
'type Test = readonly [string, number] & Readonly<{ foo: string; }>;',
],
])('handles an intersections involving a readonly array', runTests);
});

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

it.each([
['type Test = { foo: string; bar: number; } & { bar: number; };'],
[
'type Test = { foo: string; bar: number; } & Readonly<{ bar: number; }>;',
],
[
'type Test = Readonly<{ bar: number; }> & { foo: string; bar: number; };',
],
])('handles an intersection of non fully readonly types', runTests);
});
});

describe('Conditional Types', () => {
describe('is readonly', () => {
const runTests = runTestIsReadonly;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp