Skip to content

Disable unsound T[K] rule in subtype relations #39249

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16873,7 +16873,7 @@ namespace ts {
else if (target.flags & TypeFlags.IndexedAccess) {
// A type S is related to a type T[K] if S is related to C, where C is the base
// constraint of T[K] for writing.
if (relation !== identityRelation) {
if (relation === assignableRelation || relation === comparableRelation) {
const objectType = (<IndexedAccessType>target).objectType;
const indexType = (<IndexedAccessType>target).indexType;
const baseObjectType = getBaseConstraintOfType(objectType) || objectType;
Expand Down
32 changes: 19 additions & 13 deletions tests/baselines/reference/variadicTuples1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
tests/cases/conformance/types/tuple/variadicTuples1.ts(6,48): error TS1256: A rest element must be last in a tuple type.
tests/cases/conformance/types/tuple/variadicTuples1.ts(46,5): error TS2555: Expected at least 3 arguments, but got 2.
tests/cases/conformance/types/tuple/variadicTuples1.ts(47,17): error TS2345: Argument of type '45' is not assignable to parameter of type 'boolean'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(125,9): error TS2344: Type 'V' does not satisfy the constraint 'unknown[]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(52,5): error TS2555: Expected at least 3 arguments, but got 2.
tests/cases/conformance/types/tuple/variadicTuples1.ts(53,17): error TS2345: Argument of type '45' is not assignable to parameter of type 'boolean'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(131,9): error TS2344: Type 'V' does not satisfy the constraint 'unknown[]'.
The type 'readonly unknown[]' is 'readonly' and cannot be assigned to the mutable type 'unknown[]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(143,5): error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(149,5): error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...T]'.
Target requires 2 element(s) but source may have fewer.
tests/cases/conformance/types/tuple/variadicTuples1.ts(145,5): error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...U]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(151,5): error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...U]'.
Target requires 2 element(s) but source may have fewer.
tests/cases/conformance/types/tuple/variadicTuples1.ts(146,5): error TS2322: Type '[string, ...T]' is not assignable to type '[string, ...U]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(152,5): error TS2322: Type '[string, ...T]' is not assignable to type '[string, ...U]'.
Type 'T' is not assignable to type 'U'.
'T' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint 'string[]'.
Type 'string[]' is not assignable to type 'U'.
'U' could be instantiated with an arbitrary type which could be unrelated to 'string[]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(154,5): error TS2322: Type 'readonly [...T]' is not assignable to type 'T'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(160,5): error TS2322: Type 'readonly [...T]' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'readonly [...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(156,5): error TS4104: The type 'readonly [...T]' is 'readonly' and cannot be assigned to the mutable type '[...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(163,5): error TS2322: Type 'readonly [...T]' is not assignable to type 'T'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(162,5): error TS4104: The type 'readonly [...T]' is 'readonly' and cannot be assigned to the mutable type '[...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(169,5): error TS2322: Type 'readonly [...T]' is not assignable to type 'T'.
'readonly [...T]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'readonly unknown[]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(164,5): error TS2322: Type 'T' is not assignable to type '[...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(170,5): error TS2322: Type 'T' is not assignable to type '[...T]'.
The type 'readonly unknown[]' is 'readonly' and cannot be assigned to the mutable type '[...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(165,5): error TS4104: The type 'readonly [...T]' is 'readonly' and cannot be assigned to the mutable type '[...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(297,14): error TS7019: Rest parameter 'x' implicitly has an 'any[]' type.
tests/cases/conformance/types/tuple/variadicTuples1.ts(171,5): error TS4104: The type 'readonly [...T]' is 'readonly' and cannot be assigned to the mutable type '[...T]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(303,14): error TS7019: Rest parameter 'x' implicitly has an 'any[]' type.


==== tests/cases/conformance/types/tuple/variadicTuples1.ts (13 errors) ====
Expand Down Expand Up @@ -62,6 +62,12 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(297,14): error TS7019: Re
const tc3 = concat([1, 2, 3], sa);
const tc4 = concat(sa, [1, 2, 3]); // Ideally would be [...string[], number, number, number]

function concat2<T extends readonly unknown[], U extends readonly unknown[]>(t: T, u: U) {
return [...t, ...u]; // (T[number] | U[number])[]
}

const tc5 = concat2([1, 2, 3] as const, [4, 5, 6] as const); // (1 | 2 | 3 | 4 | 5 | 6)[]

// Spread arguments

declare function foo1(a: number, b: string, c: boolean, ...d: number[]): void;
Expand All @@ -74,7 +80,7 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(297,14): error TS7019: Re
foo1(...t1); // Error
~~~~~~~~~~~
!!! error TS2555: Expected at least 3 arguments, but got 2.
!!! related TS6210 tests/cases/conformance/types/tuple/variadicTuples1.ts:39:45: An argument for 'c' was not provided.
!!! related TS6210 tests/cases/conformance/types/tuple/variadicTuples1.ts:45:45: An argument for 'c' was not provided.
foo1(...t1, 45); // Error
~~
!!! error TS2345: Argument of type '45' is not assignable to parameter of type 'boolean'.
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/variadicTuples1.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const tc2 = concat(['hello'], [42]);
const tc3 = concat([1, 2, 3], sa);
const tc4 = concat(sa, [1, 2, 3]); // Ideally would be [...string[], number, number, number]

function concat2<T extends readonly unknown[], U extends readonly unknown[]>(t: T, u: U) {
return [...t, ...u]; // (T[number] | U[number])[]
}

const tc5 = concat2([1, 2, 3] as const, [4, 5, 6] as const); // (1 | 2 | 3 | 4 | 5 | 6)[]

// Spread arguments

declare function foo1(a: number, b: string, c: boolean, ...d: number[]): void;
Expand Down Expand Up @@ -320,6 +326,10 @@ var tc1 = concat([], []);
var tc2 = concat(['hello'], [42]);
var tc3 = concat([1, 2, 3], sa);
var tc4 = concat(sa, [1, 2, 3]); // Ideally would be [...string[], number, number, number]
function concat2(t, u) {
return __spreadArrays(t, u); // (T[number] | U[number])[]
}
var tc5 = concat2([1, 2, 3], [4, 5, 6]); // (1 | 2 | 3 | 4 | 5 | 6)[]
function foo2(t1, t2, a1) {
foo1(1, 'abc', true, 42, 43, 44);
foo1.apply(void 0, __spreadArrays(t1, [true, 42, 43, 44]));
Expand Down Expand Up @@ -483,6 +493,8 @@ declare const tc1: [];
declare const tc2: [string, number];
declare const tc3: [number, number, number, ...string[]];
declare const tc4: (string | number)[];
declare function concat2<T extends readonly unknown[], U extends readonly unknown[]>(t: T, u: U): (T[number] | U[number])[];
declare const tc5: (2 | 4 | 1 | 3 | 6 | 5)[];
declare function foo1(a: number, b: string, c: boolean, ...d: number[]): void;
declare function foo2(t1: [number, string], t2: [boolean], a1: number[]): void;
declare function foo3<T extends unknown[]>(x: number, ...args: [...T, number]): T;
Expand Down
Loading