Skip to content

Commit 73d37f3

Browse files
committed
Expand exception to contravariant constraint elision to all type variables
1 parent 9705547 commit 73d37f3

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12930,7 +12930,7 @@ namespace ts {
1293012930
}
1293112931
// Always substitute on type parameters, regardless of variance, since even
1293212932
// in contravarrying positions, they may be reliant on subtuted constraints to be valid
12933-
if ((covariant || type.flags & TypeFlags.TypeParameter) && parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
12933+
if ((covariant || type.flags & TypeFlags.TypeVariable) && parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
1293412934
const constraint = getImpliedConstraint(type, (<ConditionalTypeNode>parent).checkType, (<ConditionalTypeNode>parent).extendsType);
1293512935
if (constraint) {
1293612936
constraints = append(constraints, constraint);

tests/baselines/reference/callOfConditionalTypeWithConcreteBranches.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@ fn2<number>(m => m(42));
3232
// webidl-conversions example where substituion must occur, despite contravariance of the position
3333
// due to the invariant usage in `Parameters`
3434
35-
type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;
35+
type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;
36+
37+
// vscode - another `Parameters` example
38+
export type AddFirstParameterToFunctions<Target> = {
39+
[K in keyof Target]: Target[K] extends (...args: any[]) => void
40+
? (...args: Parameters<Target[K]>) => void
41+
: void
42+
};
3643

3744
//// [callOfConditionalTypeWithConcreteBranches.js]
45+
"use strict";
46+
exports.__esModule = true;
3847
function fn(arg) {
3948
// Expected: OK
4049
// Actual: Cannot convert 10 to number & T

tests/baselines/reference/callOfConditionalTypeWithConcreteBranches.symbols

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,23 @@ type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void
103103
>V : Symbol(V, Decl(callOfConditionalTypeWithConcreteBranches.ts, 33, 7))
104104
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
105105

106+
// vscode - another `Parameters` example
107+
export type AddFirstParameterToFunctions<Target> = {
108+
>AddFirstParameterToFunctions : Symbol(AddFirstParameterToFunctions, Decl(callOfConditionalTypeWithConcreteBranches.ts, 33, 92))
109+
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))
110+
111+
[K in keyof Target]: Target[K] extends (...args: any[]) => void
112+
>K : Symbol(K, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 3))
113+
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))
114+
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))
115+
>K : Symbol(K, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 3))
116+
>args : Symbol(args, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 42))
117+
118+
? (...args: Parameters<Target[K]>) => void
119+
>args : Symbol(args, Decl(callOfConditionalTypeWithConcreteBranches.ts, 38, 9))
120+
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --))
121+
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))
122+
>K : Symbol(K, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 3))
123+
124+
: void
125+
};

tests/baselines/reference/callOfConditionalTypeWithConcreteBranches.types

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,15 @@ type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void
9797
>args : any[]
9898
>args : Parameters<V>
9999

100+
// vscode - another `Parameters` example
101+
export type AddFirstParameterToFunctions<Target> = {
102+
>AddFirstParameterToFunctions : AddFirstParameterToFunctions<Target>
103+
104+
[K in keyof Target]: Target[K] extends (...args: any[]) => void
105+
>args : any[]
106+
107+
? (...args: Parameters<Target[K]>) => void
108+
>args : Parameters<Target[K]>
109+
110+
: void
111+
};

tests/cases/compiler/callOfConditionalTypeWithConcreteBranches.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ fn2<number>(m => m(42));
3131
// webidl-conversions example where substituion must occur, despite contravariance of the position
3232
// due to the invariant usage in `Parameters`
3333

34-
type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;
34+
type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;
35+
36+
// vscode - another `Parameters` example
37+
export type AddFirstParameterToFunctions<Target> = {
38+
[K in keyof Target]: Target[K] extends (...args: any[]) => void
39+
? (...args: Parameters<Target[K]>) => void
40+
: void
41+
};

0 commit comments

Comments
 (0)