Skip to content

Commit fec691e

Browse files
Merge pull request #6355 from Microsoft/contextuallyTypeAndAndComma
Contextually type '&&' and the comma operator
2 parents 9a9aa00 + 7ce819f commit fec691e

21 files changed

+237
-0
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7362,6 +7362,12 @@ namespace ts {
73627362
}
73637363
return type;
73647364
}
7365+
else if (operator === SyntaxKind.AmpersandAmpersandToken || operator === SyntaxKind.CommaToken) {
7366+
if (node === binaryExpression.right) {
7367+
return getContextualType(binaryExpression);
7368+
}
7369+
}
7370+
73657371
return undefined;
73667372
}
73677373

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [contextuallyTypeCommaOperator01.ts]
2+
3+
let x: (a: string) => string;
4+
5+
x = (100, a => a);
6+
7+
//// [contextuallyTypeCommaOperator01.js]
8+
var x;
9+
x = (100, function (a) { return a; });
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator01.ts ===
2+
3+
let x: (a: string) => string;
4+
>x : Symbol(x, Decl(contextuallyTypeCommaOperator01.ts, 1, 3))
5+
>a : Symbol(a, Decl(contextuallyTypeCommaOperator01.ts, 1, 8))
6+
7+
x = (100, a => a);
8+
>x : Symbol(x, Decl(contextuallyTypeCommaOperator01.ts, 1, 3))
9+
>a : Symbol(a, Decl(contextuallyTypeCommaOperator01.ts, 3, 9))
10+
>a : Symbol(a, Decl(contextuallyTypeCommaOperator01.ts, 3, 9))
11+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator01.ts ===
2+
3+
let x: (a: string) => string;
4+
>x : (a: string) => string
5+
>a : string
6+
7+
x = (100, a => a);
8+
>x = (100, a => a) : (a: string) => string
9+
>x : (a: string) => string
10+
>(100, a => a) : (a: string) => string
11+
>100, a => a : (a: string) => string
12+
>100 : number
13+
>a => a : (a: string) => string
14+
>a : string
15+
>a : string
16+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts(4,1): error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'.
2+
Type 'number' is not assignable to type 'string'.
3+
tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts(5,11): error TS2322: Type 'string' is not assignable to type 'number'.
4+
5+
6+
==== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts (2 errors) ====
7+
8+
let x: (a: string) => string;
9+
10+
x = (100, a => {
11+
~
12+
!!! error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'.
13+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
14+
const b: number = a;
15+
~
16+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
17+
return b;
18+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [contextuallyTypeCommaOperator02.ts]
2+
3+
let x: (a: string) => string;
4+
5+
x = (100, a => {
6+
const b: number = a;
7+
return b;
8+
});
9+
10+
//// [contextuallyTypeCommaOperator02.js]
11+
var x;
12+
x = (100, function (a) {
13+
var b = a;
14+
return b;
15+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator03.ts(4,6): error TS7006: Parameter 'a' implicitly has an 'any' type.
2+
3+
4+
==== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator03.ts (1 errors) ====
5+
6+
let x: (a: string) => string;
7+
8+
x = (a => a, b => b);
9+
~
10+
!!! error TS7006: Parameter 'a' implicitly has an 'any' type.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [contextuallyTypeCommaOperator03.ts]
2+
3+
let x: (a: string) => string;
4+
5+
x = (a => a, b => b);
6+
7+
//// [contextuallyTypeCommaOperator03.js]
8+
var x;
9+
x = (function (a) { return a; }, function (b) { return b; });
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [contextuallyTypeLogicalAnd01.ts]
2+
3+
let x: (a: string) => string;
4+
let y = true;
5+
6+
x = y && (a => a);
7+
8+
//// [contextuallyTypeLogicalAnd01.js]
9+
var x;
10+
var y = true;
11+
x = y && (function (a) { return a; });
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd01.ts ===
2+
3+
let x: (a: string) => string;
4+
>x : Symbol(x, Decl(contextuallyTypeLogicalAnd01.ts, 1, 3))
5+
>a : Symbol(a, Decl(contextuallyTypeLogicalAnd01.ts, 1, 8))
6+
7+
let y = true;
8+
>y : Symbol(y, Decl(contextuallyTypeLogicalAnd01.ts, 2, 3))
9+
10+
x = y && (a => a);
11+
>x : Symbol(x, Decl(contextuallyTypeLogicalAnd01.ts, 1, 3))
12+
>y : Symbol(y, Decl(contextuallyTypeLogicalAnd01.ts, 2, 3))
13+
>a : Symbol(a, Decl(contextuallyTypeLogicalAnd01.ts, 4, 10))
14+
>a : Symbol(a, Decl(contextuallyTypeLogicalAnd01.ts, 4, 10))
15+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd01.ts ===
2+
3+
let x: (a: string) => string;
4+
>x : (a: string) => string
5+
>a : string
6+
7+
let y = true;
8+
>y : boolean
9+
>true : boolean
10+
11+
x = y && (a => a);
12+
>x = y && (a => a) : (a: string) => string
13+
>x : (a: string) => string
14+
>y && (a => a) : (a: string) => string
15+
>y : boolean
16+
>(a => a) : (a: string) => string
17+
>a => a : (a: string) => string
18+
>a : string
19+
>a : string
20+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts(5,1): error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'.
2+
Type 'number' is not assignable to type 'string'.
3+
tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts(6,11): error TS2322: Type 'string' is not assignable to type 'number'.
4+
5+
6+
==== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts (2 errors) ====
7+
8+
let x: (a: string) => string;
9+
let y = true;
10+
11+
x = y && (a => {
12+
~
13+
!!! error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'.
14+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
15+
const b: number = a;
16+
~
17+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
18+
return b;
19+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [contextuallyTypeLogicalAnd02.ts]
2+
3+
let x: (a: string) => string;
4+
let y = true;
5+
6+
x = y && (a => {
7+
const b: number = a;
8+
return b;
9+
});
10+
11+
//// [contextuallyTypeLogicalAnd02.js]
12+
var x;
13+
var y = true;
14+
x = y && (function (a) {
15+
var b = a;
16+
return b;
17+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd03.ts(5,6): error TS7006: Parameter 'a' implicitly has an 'any' type.
2+
3+
4+
==== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd03.ts (1 errors) ====
5+
6+
let x: (a: string) => string;
7+
let y = true;
8+
9+
x = (a => a) && (b => b);
10+
~
11+
!!! error TS7006: Parameter 'a' implicitly has an 'any' type.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [contextuallyTypeLogicalAnd03.ts]
2+
3+
let x: (a: string) => string;
4+
let y = true;
5+
6+
x = (a => a) && (b => b);
7+
8+
//// [contextuallyTypeLogicalAnd03.js]
9+
var x;
10+
var y = true;
11+
x = (function (a) { return a; }) && (function (b) { return b; });
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @noImplicitAny: true
2+
3+
let x: (a: string) => string;
4+
5+
x = (100, a => a);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @noImplicitAny: true
2+
3+
let x: (a: string) => string;
4+
5+
x = (100, a => {
6+
const b: number = a;
7+
return b;
8+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @noImplicitAny: true
2+
3+
let x: (a: string) => string;
4+
5+
x = (a => a, b => b);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @noImplicitAny: true
2+
3+
let x: (a: string) => string;
4+
let y = true;
5+
6+
x = y && (a => a);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @noImplicitAny: true
2+
3+
let x: (a: string) => string;
4+
let y = true;
5+
6+
x = y && (a => {
7+
const b: number = a;
8+
return b;
9+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @noImplicitAny: true
2+
3+
let x: (a: string) => string;
4+
let y = true;
5+
6+
x = (a => a) && (b => b);

0 commit comments

Comments
 (0)