Skip to content

Commit 5f7155d

Browse files
committed
Ensure rest type for source parameter is readonly
1 parent 5eec44a commit 5f7155d

File tree

4 files changed

+18
-50
lines changed

4 files changed

+18
-50
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19810,7 +19810,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1981019810
const restIndex = sourceRestType || targetRestType ? paramCount - 1 : -1;
1981119811

1981219812
for (let i = 0; i < paramCount; i++) {
19813-
const sourceType = i === restIndex ? getRestTypeAtPosition(source, i) : tryGetTypeAtPosition(source, i);
19813+
const sourceType = i === restIndex ? getRestTypeAtPosition(source, i, /*readonly*/ true) : tryGetTypeAtPosition(source, i);
1981419814
const targetType = i === restIndex ? getRestTypeAtPosition(target, i) : tryGetTypeAtPosition(target, i);
1981519815
if (sourceType && targetType) {
1981619816
// In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
@@ -34565,12 +34565,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3456534565
return undefined;
3456634566
}
3456734567

34568-
function getRestTypeAtPosition(source: Signature, pos: number): Type {
34568+
function getRestTypeAtPosition(source: Signature, pos: number, readonly = false): Type {
3456934569
const parameterCount = getParameterCount(source);
3457034570
const minArgumentCount = getMinArgumentCount(source);
3457134571
const restType = getEffectiveRestType(source);
3457234572
if (restType && pos >= parameterCount - 1) {
34573-
return pos === parameterCount - 1 ? restType : createArrayType(getIndexedAccessType(restType, numberType));
34573+
return pos === parameterCount - 1 ? restType : createArrayType(getIndexedAccessType(restType, numberType), readonly);
3457434574
}
3457534575
const types = [];
3457634576
const flags = [];
@@ -34589,7 +34589,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3458934589
names.push(name);
3459034590
}
3459134591
}
34592-
return createTupleType(types, flags, /*readonly*/ false, length(names) === length(types) ? names : undefined);
34592+
return createTupleType(types, flags, readonly, length(names) === length(types) ? names : undefined);
3459334593
}
3459434594

3459534595
// Return the number of parameters in a signature. The rest parameter, if present, counts as one

tests/baselines/reference/contextualTupleTypeParameterReadonly.errors.txt

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/baselines/reference/genericRestParameters3.errors.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(18,1): error TS2345
66
Source has 0 element(s) but target requires 2.
77
tests/cases/conformance/types/rest/genericRestParameters3.ts(23,1): error TS2322: Type '(x: string, y: string) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
88
Types of parameters 'y' and 'args' are incompatible.
9-
Type '[string] | [number, boolean]' is not assignable to type '[y: string]'.
10-
Type '[number, boolean]' is not assignable to type '[y: string]'.
9+
Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: string]'.
10+
Type '[number, boolean]' is not assignable to type 'readonly [y: string]'.
1111
Source has 2 element(s) but target allows only 1.
1212
tests/cases/conformance/types/rest/genericRestParameters3.ts(24,1): error TS2322: Type '(x: string, y: number, z: boolean) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
1313
Types of parameters 'y' and 'args' are incompatible.
14-
Type '[string] | [number, boolean]' is not assignable to type '[y: number, z: boolean]'.
15-
Type '[string]' is not assignable to type '[y: number, z: boolean]'.
14+
Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: number, z: boolean]'.
15+
Type '[string]' is not assignable to type 'readonly [y: number, z: boolean]'.
1616
Source has 1 element(s) but target requires 2.
1717
tests/cases/conformance/types/rest/genericRestParameters3.ts(35,1): error TS2554: Expected 1 arguments, but got 0.
1818
tests/cases/conformance/types/rest/genericRestParameters3.ts(36,21): error TS2345: Argument of type 'number' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
1919
tests/cases/conformance/types/rest/genericRestParameters3.ts(37,21): error TS2345: Argument of type '<T extends any[]>(cb: (...args: T) => void) => void' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
2020
Types of parameters 'cb' and 'args' are incompatible.
21-
Property '0' is missing in type 'CoolArray<any>' but required in type '[cb: (...args: any[]) => void]'.
21+
Property '0' is missing in type 'CoolArray<any>' but required in type 'readonly [cb: (...args: any[]) => void]'.
2222
tests/cases/conformance/types/rest/genericRestParameters3.ts(44,32): error TS2345: Argument of type '[10, 20]' is not assignable to parameter of type 'CoolArray<number>'.
2323
Property 'hello' is missing in type '[10, 20]' but required in type 'CoolArray<number>'.
2424
tests/cases/conformance/types/rest/genericRestParameters3.ts(49,1): error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
@@ -69,15 +69,15 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(59,5): error TS2345
6969
~~
7070
!!! error TS2322: Type '(x: string, y: string) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
7171
!!! error TS2322: Types of parameters 'y' and 'args' are incompatible.
72-
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type '[y: string]'.
73-
!!! error TS2322: Type '[number, boolean]' is not assignable to type '[y: string]'.
72+
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: string]'.
73+
!!! error TS2322: Type '[number, boolean]' is not assignable to type 'readonly [y: string]'.
7474
!!! error TS2322: Source has 2 element(s) but target allows only 1.
7575
f1 = f3; // Error
7676
~~
7777
!!! error TS2322: Type '(x: string, y: number, z: boolean) => void' is not assignable to type '(x: string, ...args: [string] | [number, boolean]) => void'.
7878
!!! error TS2322: Types of parameters 'y' and 'args' are incompatible.
79-
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type '[y: number, z: boolean]'.
80-
!!! error TS2322: Type '[string]' is not assignable to type '[y: number, z: boolean]'.
79+
!!! error TS2322: Type '[string] | [number, boolean]' is not assignable to type 'readonly [y: number, z: boolean]'.
80+
!!! error TS2322: Type '[string]' is not assignable to type 'readonly [y: number, z: boolean]'.
8181
!!! error TS2322: Source has 1 element(s) but target requires 2.
8282
f1 = f4;
8383

@@ -100,7 +100,7 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(59,5): error TS2345
100100
~~~
101101
!!! error TS2345: Argument of type '<T extends any[]>(cb: (...args: T) => void) => void' is not assignable to parameter of type '(...args: CoolArray<any>) => void'.
102102
!!! error TS2345: Types of parameters 'cb' and 'args' are incompatible.
103-
!!! error TS2345: Property '0' is missing in type 'CoolArray<any>' but required in type '[cb: (...args: any[]) => void]'.
103+
!!! error TS2345: Property '0' is missing in type 'CoolArray<any>' but required in type 'readonly [cb: (...args: any[]) => void]'.
104104

105105
function bar<T extends any[]>(...args: T): T {
106106
return args;

tests/baselines/reference/restTuplesFromContextualTypes.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error TS2345: Argument of type '(a: number, b: T[0], ...x: T[number][]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
22
Types of parameters 'b' and 'args' are incompatible.
3-
Type 'T' is not assignable to type '[b: T[0], ...x: T[number][]]'.
4-
Type 'any[]' is not assignable to type '[b: T[0], ...x: T[number][]]'.
3+
Type 'T' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
4+
Type 'any[]' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
55
Source provides no match for required element at position 0 in target.
66

77

@@ -65,8 +65,8 @@ tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error
6565
~~~~~~~~~~~~~~~~~~
6666
!!! error TS2345: Argument of type '(a: number, b: T[0], ...x: T[number][]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
6767
!!! error TS2345: Types of parameters 'b' and 'args' are incompatible.
68-
!!! error TS2345: Type 'T' is not assignable to type '[b: T[0], ...x: T[number][]]'.
69-
!!! error TS2345: Type 'any[]' is not assignable to type '[b: T[0], ...x: T[number][]]'.
68+
!!! error TS2345: Type 'T' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
69+
!!! error TS2345: Type 'any[]' is not assignable to type 'readonly [b: T[0], ...x: T[number][]]'.
7070
!!! error TS2345: Source provides no match for required element at position 0 in target.
7171
}
7272

0 commit comments

Comments
 (0)