Skip to content

Commit 9ada915

Browse files
authored
Merge pull request #15381 from Microsoft/revert15104
Revert #15104
2 parents 8a85f4a + fe0a307 commit 9ada915

21 files changed

+154
-727
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8177,8 +8177,7 @@ namespace ts {
81778177
function isSignatureAssignableTo(source: Signature,
81788178
target: Signature,
81798179
ignoreReturnTypes: boolean): boolean {
8180-
return compareSignaturesRelated(source, target, /*checkAsCallback*/ false, ignoreReturnTypes, /*reportErrors*/ false,
8181-
/*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False;
8180+
return compareSignaturesRelated(source, target, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False;
81828181
}
81838182

81848183
type ErrorReporter = (message: DiagnosticMessage, arg0?: string, arg1?: string) => void;
@@ -8188,7 +8187,6 @@ namespace ts {
81888187
*/
81898188
function compareSignaturesRelated(source: Signature,
81908189
target: Signature,
8191-
checkAsCallback: boolean,
81928190
ignoreReturnTypes: boolean,
81938191
reportErrors: boolean,
81948192
errorReporter: ErrorReporter,
@@ -8231,23 +8229,9 @@ namespace ts {
82318229
const sourceParams = source.parameters;
82328230
const targetParams = target.parameters;
82338231
for (let i = 0; i < checkCount; i++) {
8234-
const sourceType = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source);
8235-
const targetType = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target);
8236-
const sourceSig = getSingleCallSignature(getNonNullableType(sourceType));
8237-
const targetSig = getSingleCallSignature(getNonNullableType(targetType));
8238-
// In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
8239-
// how Foo uses T, we need to relate parameters bi-variantly (given that parameters are input positions,
8240-
// they naturally relate only contra-variantly). However, if the source and target parameters both have
8241-
// function types with a single call signature, we known we are relating two callback parameters. In
8242-
// that case it is sufficient to only relate the parameters of the signatures co-variantly because,
8243-
// similar to return values, callback parameters are output positions. This means that a Promise<T>,
8244-
// where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant)
8245-
// with respect to T.
8246-
const callbacks = sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate &&
8247-
(getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable);
8248-
const related = callbacks ?
8249-
compareSignaturesRelated(targetSig, sourceSig, /*checkAsCallback*/ true, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) :
8250-
!checkAsCallback && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors);
8232+
const s = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source);
8233+
const t = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target);
8234+
const related = compareTypes(s, t, /*reportErrors*/ false) || compareTypes(t, s, reportErrors);
82518235
if (!related) {
82528236
if (reportErrors) {
82538237
errorReporter(Diagnostics.Types_of_parameters_0_and_1_are_incompatible,
@@ -8279,11 +8263,7 @@ namespace ts {
82798263
}
82808264
}
82818265
else {
8282-
// When relating callback signatures, we still need to relate return types bi-variantly as otherwise
8283-
// the containing type wouldn't be co-variant. For example, interface Foo<T> { add(cb: () => T): void }
8284-
// wouldn't be co-variant for T without this rule.
8285-
result &= checkAsCallback && compareTypes(targetReturnType, sourceReturnType, /*reportErrors*/ false) ||
8286-
compareTypes(sourceReturnType, targetReturnType, reportErrors);
8266+
result &= compareTypes(sourceReturnType, targetReturnType, reportErrors);
82878267
}
82888268

82898269
}
@@ -9251,7 +9231,7 @@ namespace ts {
92519231
* See signatureAssignableTo, compareSignaturesIdentical
92529232
*/
92539233
function signatureRelatedTo(source: Signature, target: Signature, reportErrors: boolean): Ternary {
9254-
return compareSignaturesRelated(source, target, /*checkAsCallback*/ false, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo);
9234+
return compareSignaturesRelated(source, target, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo);
92559235
}
92569236

92579237
function signaturesIdenticalTo(source: Type, target: Type, kind: SignatureKind): Ternary {

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace ts {
5757
// The visitXXX functions could be written as local functions that close over the cbNode and cbNodeArray
5858
// callback parameters, but that causes a closure allocation for each invocation with noticeable effects
5959
// on performance.
60-
const visitNodes: (cb: ((node: Node) => T) | ((node: Node[]) => T), nodes: Node[]) => T = cbNodeArray ? visitNodeArray : visitEachNode;
60+
const visitNodes: (cb: (node: Node | Node[]) => T, nodes: Node[]) => T = cbNodeArray ? visitNodeArray : visitEachNode;
6161
const cbNodes = cbNodeArray || cbNode;
6262
switch (node.kind) {
6363
case SyntaxKind.QualifiedName:

src/compiler/visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ namespace ts {
885885
return initial;
886886
}
887887

888-
const reduceNodes: (nodes: NodeArray<Node>, f: ((memo: T, node: Node) => T) | ((memo: T, node: NodeArray<Node>) => T), initial: T) => T = cbNodeArray ? reduceNodeArray : reduceLeft;
888+
const reduceNodes: (nodes: NodeArray<Node>, f: (memo: T, node: Node | NodeArray<Node>) => T, initial: T) => T = cbNodeArray ? reduceNodeArray : reduceLeft;
889889
const cbNodes = cbNodeArray || cbNode;
890890
const kind = node.kind;
891891

tests/baselines/reference/assignmentCompatWithCallSignatures3.errors.txt

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

tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(52,9): error TS2322: Type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived'.
22
Types of parameters 'y' and 'y' are incompatible.
3-
Types of parameters 'arg2' and 'arg2' are incompatible.
4-
Type '{ foo: number; }' is not assignable to type 'Base'.
5-
Types of property 'foo' are incompatible.
6-
Type 'number' is not assignable to type 'string'.
3+
Type '(arg2: Base) => Derived' is not assignable to type '(arg2: { foo: number; }) => any'.
4+
Types of parameters 'arg2' and 'arg2' are incompatible.
5+
Type '{ foo: number; }' is not assignable to type 'Base'.
6+
Types of property 'foo' are incompatible.
7+
Type 'number' is not assignable to type 'string'.
78
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(53,9): error TS2322: Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U'.
89
Types of parameters 'y' and 'y' are incompatible.
9-
Types of parameters 'arg2' and 'arg2' are incompatible.
10-
Type 'Base' is not assignable to type '{ foo: number; }'.
11-
Types of property 'foo' are incompatible.
12-
Type 'string' is not assignable to type 'number'.
10+
Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived'.
11+
Types of parameters 'arg2' and 'arg2' are incompatible.
12+
Type 'Base' is not assignable to type '{ foo: number; }'.
13+
Types of property 'foo' are incompatible.
14+
Type 'string' is not assignable to type 'number'.
1315

1416

1517
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts (2 errors) ====
@@ -68,18 +70,20 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
6870
~~
6971
!!! error TS2322: Type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U' is not assignable to type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived'.
7072
!!! error TS2322: Types of parameters 'y' and 'y' are incompatible.
71-
!!! error TS2322: Types of parameters 'arg2' and 'arg2' are incompatible.
72-
!!! error TS2322: Type '{ foo: number; }' is not assignable to type 'Base'.
73-
!!! error TS2322: Types of property 'foo' are incompatible.
74-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
73+
!!! error TS2322: Type '(arg2: Base) => Derived' is not assignable to type '(arg2: { foo: number; }) => any'.
74+
!!! error TS2322: Types of parameters 'arg2' and 'arg2' are incompatible.
75+
!!! error TS2322: Type '{ foo: number; }' is not assignable to type 'Base'.
76+
!!! error TS2322: Types of property 'foo' are incompatible.
77+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
7578
b8 = a8; // error, { foo: number } and Base are incompatible
7679
~~
7780
!!! error TS2322: Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: number; }) => U) => (r: T) => U'.
7881
!!! error TS2322: Types of parameters 'y' and 'y' are incompatible.
79-
!!! error TS2322: Types of parameters 'arg2' and 'arg2' are incompatible.
80-
!!! error TS2322: Type 'Base' is not assignable to type '{ foo: number; }'.
81-
!!! error TS2322: Types of property 'foo' are incompatible.
82-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
82+
!!! error TS2322: Type '(arg2: { foo: number; }) => any' is not assignable to type '(arg2: Base) => Derived'.
83+
!!! error TS2322: Types of parameters 'arg2' and 'arg2' are incompatible.
84+
!!! error TS2322: Type 'Base' is not assignable to type '{ foo: number; }'.
85+
!!! error TS2322: Types of property 'foo' are incompatible.
86+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
8387

8488

8589
var b10: <T extends Derived>(...x: T[]) => T;

0 commit comments

Comments
 (0)