Skip to content

Expand exception to contravariant constraint elision to all type variables #43599

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 3 commits into from
Apr 14, 2021
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
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12929,8 +12929,8 @@ namespace ts {
covariant = !covariant;
}
// Always substitute on type parameters, regardless of variance, since even
// in contravarrying positions, they may be reliant on subtuted constraints to be valid
if ((covariant || type.flags & TypeFlags.TypeParameter) && parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
// in contravariant positions, they may rely on substituted constraints to be valid
if ((covariant || type.flags & TypeFlags.TypeVariable) && parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
const constraint = getImpliedConstraint(type, (<ConditionalTypeNode>parent).checkType, (<ConditionalTypeNode>parent).extendsType);
if (constraint) {
constraints = append(constraints, constraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@ fn2<number>(m => m(42));
// webidl-conversions example where substituion must occur, despite contravariance of the position
// due to the invariant usage in `Parameters`

type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;
type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;

// vscode - another `Parameters` example
export type AddFirstParameterToFunctions<Target> = {
[K in keyof Target]: Target[K] extends (...args: any[]) => void
? (...args: Parameters<Target[K]>) => void
: void
};

//// [callOfConditionalTypeWithConcreteBranches.js]
"use strict";
exports.__esModule = true;
function fn(arg) {
// Expected: OK
// Actual: Cannot convert 10 to number & T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,23 @@ type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void
>V : Symbol(V, Decl(callOfConditionalTypeWithConcreteBranches.ts, 33, 7))
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

// vscode - another `Parameters` example
export type AddFirstParameterToFunctions<Target> = {
>AddFirstParameterToFunctions : Symbol(AddFirstParameterToFunctions, Decl(callOfConditionalTypeWithConcreteBranches.ts, 33, 92))
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))

[K in keyof Target]: Target[K] extends (...args: any[]) => void
>K : Symbol(K, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 3))
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))
>K : Symbol(K, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 3))
>args : Symbol(args, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 42))

? (...args: Parameters<Target[K]>) => void
>args : Symbol(args, Decl(callOfConditionalTypeWithConcreteBranches.ts, 38, 9))
>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --))
>Target : Symbol(Target, Decl(callOfConditionalTypeWithConcreteBranches.ts, 36, 41))
>K : Symbol(K, Decl(callOfConditionalTypeWithConcreteBranches.ts, 37, 3))

: void
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,15 @@ type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void
>args : any[]
>args : Parameters<V>

// vscode - another `Parameters` example
export type AddFirstParameterToFunctions<Target> = {
>AddFirstParameterToFunctions : AddFirstParameterToFunctions<Target>

[K in keyof Target]: Target[K] extends (...args: any[]) => void
>args : any[]

? (...args: Parameters<Target[K]>) => void
>args : Parameters<Target[K]>

: void
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ fn2<number>(m => m(42));
// webidl-conversions example where substituion must occur, despite contravariance of the position
// due to the invariant usage in `Parameters`

type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;
type X<V> = V extends (...args: any[]) => any ? (...args: Parameters<V>) => void : Function;

// vscode - another `Parameters` example
export type AddFirstParameterToFunctions<Target> = {
[K in keyof Target]: Target[K] extends (...args: any[]) => void
? (...args: Parameters<Target[K]>) => void
: void
};