diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c2e798659d..fb4af5d729 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7362,6 +7362,12 @@ namespace ts { } return type; } + else if (operator === SyntaxKind.AmpersandAmpersandToken || operator === SyntaxKind.CommaToken) { + if (node === binaryExpression.right) { + return getContextualType(binaryExpression); + } + } + return undefined; } diff --git a/tests/baselines/reference/contextuallyTypeCommaOperator01.js b/tests/baselines/reference/contextuallyTypeCommaOperator01.js new file mode 100644 index 0000000000..0da3ac9100 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeCommaOperator01.js @@ -0,0 +1,9 @@ +//// [contextuallyTypeCommaOperator01.ts] + +let x: (a: string) => string; + +x = (100, a => a); + +//// [contextuallyTypeCommaOperator01.js] +var x; +x = (100, function (a) { return a; }); diff --git a/tests/baselines/reference/contextuallyTypeCommaOperator01.symbols b/tests/baselines/reference/contextuallyTypeCommaOperator01.symbols new file mode 100644 index 0000000000..c07fd28ae7 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeCommaOperator01.symbols @@ -0,0 +1,11 @@ +=== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator01.ts === + +let x: (a: string) => string; +>x : Symbol(x, Decl(contextuallyTypeCommaOperator01.ts, 1, 3)) +>a : Symbol(a, Decl(contextuallyTypeCommaOperator01.ts, 1, 8)) + +x = (100, a => a); +>x : Symbol(x, Decl(contextuallyTypeCommaOperator01.ts, 1, 3)) +>a : Symbol(a, Decl(contextuallyTypeCommaOperator01.ts, 3, 9)) +>a : Symbol(a, Decl(contextuallyTypeCommaOperator01.ts, 3, 9)) + diff --git a/tests/baselines/reference/contextuallyTypeCommaOperator01.types b/tests/baselines/reference/contextuallyTypeCommaOperator01.types new file mode 100644 index 0000000000..c3b7b700d6 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeCommaOperator01.types @@ -0,0 +1,16 @@ +=== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator01.ts === + +let x: (a: string) => string; +>x : (a: string) => string +>a : string + +x = (100, a => a); +>x = (100, a => a) : (a: string) => string +>x : (a: string) => string +>(100, a => a) : (a: string) => string +>100, a => a : (a: string) => string +>100 : number +>a => a : (a: string) => string +>a : string +>a : string + diff --git a/tests/baselines/reference/contextuallyTypeCommaOperator02.errors.txt b/tests/baselines/reference/contextuallyTypeCommaOperator02.errors.txt new file mode 100644 index 0000000000..3214f7e502 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeCommaOperator02.errors.txt @@ -0,0 +1,18 @@ +tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts(4,1): error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'. + Type 'number' is not assignable to type 'string'. +tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts(5,11): error TS2322: Type 'string' is not assignable to type 'number'. + + +==== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts (2 errors) ==== + + let x: (a: string) => string; + + x = (100, a => { + ~ +!!! error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'. +!!! error TS2322: Type 'number' is not assignable to type 'string'. + const b: number = a; + ~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. + return b; + }); \ No newline at end of file diff --git a/tests/baselines/reference/contextuallyTypeCommaOperator02.js b/tests/baselines/reference/contextuallyTypeCommaOperator02.js new file mode 100644 index 0000000000..0f8d67dd0e --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeCommaOperator02.js @@ -0,0 +1,15 @@ +//// [contextuallyTypeCommaOperator02.ts] + +let x: (a: string) => string; + +x = (100, a => { + const b: number = a; + return b; +}); + +//// [contextuallyTypeCommaOperator02.js] +var x; +x = (100, function (a) { + var b = a; + return b; +}); diff --git a/tests/baselines/reference/contextuallyTypeCommaOperator03.errors.txt b/tests/baselines/reference/contextuallyTypeCommaOperator03.errors.txt new file mode 100644 index 0000000000..715b7cf70e --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeCommaOperator03.errors.txt @@ -0,0 +1,10 @@ +tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator03.ts(4,6): error TS7006: Parameter 'a' implicitly has an 'any' type. + + +==== tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator03.ts (1 errors) ==== + + let x: (a: string) => string; + + x = (a => a, b => b); + ~ +!!! error TS7006: Parameter 'a' implicitly has an 'any' type. \ No newline at end of file diff --git a/tests/baselines/reference/contextuallyTypeCommaOperator03.js b/tests/baselines/reference/contextuallyTypeCommaOperator03.js new file mode 100644 index 0000000000..c4754f28c4 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeCommaOperator03.js @@ -0,0 +1,9 @@ +//// [contextuallyTypeCommaOperator03.ts] + +let x: (a: string) => string; + +x = (a => a, b => b); + +//// [contextuallyTypeCommaOperator03.js] +var x; +x = (function (a) { return a; }, function (b) { return b; }); diff --git a/tests/baselines/reference/contextuallyTypeLogicalAnd01.js b/tests/baselines/reference/contextuallyTypeLogicalAnd01.js new file mode 100644 index 0000000000..b8b1f0d1e0 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeLogicalAnd01.js @@ -0,0 +1,11 @@ +//// [contextuallyTypeLogicalAnd01.ts] + +let x: (a: string) => string; +let y = true; + +x = y && (a => a); + +//// [contextuallyTypeLogicalAnd01.js] +var x; +var y = true; +x = y && (function (a) { return a; }); diff --git a/tests/baselines/reference/contextuallyTypeLogicalAnd01.symbols b/tests/baselines/reference/contextuallyTypeLogicalAnd01.symbols new file mode 100644 index 0000000000..4d69b43e66 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeLogicalAnd01.symbols @@ -0,0 +1,15 @@ +=== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd01.ts === + +let x: (a: string) => string; +>x : Symbol(x, Decl(contextuallyTypeLogicalAnd01.ts, 1, 3)) +>a : Symbol(a, Decl(contextuallyTypeLogicalAnd01.ts, 1, 8)) + +let y = true; +>y : Symbol(y, Decl(contextuallyTypeLogicalAnd01.ts, 2, 3)) + +x = y && (a => a); +>x : Symbol(x, Decl(contextuallyTypeLogicalAnd01.ts, 1, 3)) +>y : Symbol(y, Decl(contextuallyTypeLogicalAnd01.ts, 2, 3)) +>a : Symbol(a, Decl(contextuallyTypeLogicalAnd01.ts, 4, 10)) +>a : Symbol(a, Decl(contextuallyTypeLogicalAnd01.ts, 4, 10)) + diff --git a/tests/baselines/reference/contextuallyTypeLogicalAnd01.types b/tests/baselines/reference/contextuallyTypeLogicalAnd01.types new file mode 100644 index 0000000000..a405888c8a --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeLogicalAnd01.types @@ -0,0 +1,20 @@ +=== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd01.ts === + +let x: (a: string) => string; +>x : (a: string) => string +>a : string + +let y = true; +>y : boolean +>true : boolean + +x = y && (a => a); +>x = y && (a => a) : (a: string) => string +>x : (a: string) => string +>y && (a => a) : (a: string) => string +>y : boolean +>(a => a) : (a: string) => string +>a => a : (a: string) => string +>a : string +>a : string + diff --git a/tests/baselines/reference/contextuallyTypeLogicalAnd02.errors.txt b/tests/baselines/reference/contextuallyTypeLogicalAnd02.errors.txt new file mode 100644 index 0000000000..7452d333d8 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeLogicalAnd02.errors.txt @@ -0,0 +1,19 @@ +tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts(5,1): error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'. + Type 'number' is not assignable to type 'string'. +tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts(6,11): error TS2322: Type 'string' is not assignable to type 'number'. + + +==== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts (2 errors) ==== + + let x: (a: string) => string; + let y = true; + + x = y && (a => { + ~ +!!! error TS2322: Type '(a: string) => number' is not assignable to type '(a: string) => string'. +!!! error TS2322: Type 'number' is not assignable to type 'string'. + const b: number = a; + ~ +!!! error TS2322: Type 'string' is not assignable to type 'number'. + return b; + }); \ No newline at end of file diff --git a/tests/baselines/reference/contextuallyTypeLogicalAnd02.js b/tests/baselines/reference/contextuallyTypeLogicalAnd02.js new file mode 100644 index 0000000000..6dc1fa5b26 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeLogicalAnd02.js @@ -0,0 +1,17 @@ +//// [contextuallyTypeLogicalAnd02.ts] + +let x: (a: string) => string; +let y = true; + +x = y && (a => { + const b: number = a; + return b; +}); + +//// [contextuallyTypeLogicalAnd02.js] +var x; +var y = true; +x = y && (function (a) { + var b = a; + return b; +}); diff --git a/tests/baselines/reference/contextuallyTypeLogicalAnd03.errors.txt b/tests/baselines/reference/contextuallyTypeLogicalAnd03.errors.txt new file mode 100644 index 0000000000..1430be0042 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeLogicalAnd03.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd03.ts(5,6): error TS7006: Parameter 'a' implicitly has an 'any' type. + + +==== tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd03.ts (1 errors) ==== + + let x: (a: string) => string; + let y = true; + + x = (a => a) && (b => b); + ~ +!!! error TS7006: Parameter 'a' implicitly has an 'any' type. \ No newline at end of file diff --git a/tests/baselines/reference/contextuallyTypeLogicalAnd03.js b/tests/baselines/reference/contextuallyTypeLogicalAnd03.js new file mode 100644 index 0000000000..cbf1b3a319 --- /dev/null +++ b/tests/baselines/reference/contextuallyTypeLogicalAnd03.js @@ -0,0 +1,11 @@ +//// [contextuallyTypeLogicalAnd03.ts] + +let x: (a: string) => string; +let y = true; + +x = (a => a) && (b => b); + +//// [contextuallyTypeLogicalAnd03.js] +var x; +var y = true; +x = (function (a) { return a; }) && (function (b) { return b; }); diff --git a/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator01.ts b/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator01.ts new file mode 100644 index 0000000000..167369a020 --- /dev/null +++ b/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator01.ts @@ -0,0 +1,5 @@ +// @noImplicitAny: true + +let x: (a: string) => string; + +x = (100, a => a); \ No newline at end of file diff --git a/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts b/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts new file mode 100644 index 0000000000..11e743b583 --- /dev/null +++ b/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator02.ts @@ -0,0 +1,8 @@ +// @noImplicitAny: true + +let x: (a: string) => string; + +x = (100, a => { + const b: number = a; + return b; +}); \ No newline at end of file diff --git a/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator03.ts b/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator03.ts new file mode 100644 index 0000000000..321eb99d15 --- /dev/null +++ b/tests/cases/conformance/types/contextualTypes/commaOperator/contextuallyTypeCommaOperator03.ts @@ -0,0 +1,5 @@ +// @noImplicitAny: true + +let x: (a: string) => string; + +x = (a => a, b => b); \ No newline at end of file diff --git a/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd01.ts b/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd01.ts new file mode 100644 index 0000000000..30e46429fa --- /dev/null +++ b/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd01.ts @@ -0,0 +1,6 @@ +// @noImplicitAny: true + +let x: (a: string) => string; +let y = true; + +x = y && (a => a); \ No newline at end of file diff --git a/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts b/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts new file mode 100644 index 0000000000..26e58ae49e --- /dev/null +++ b/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd02.ts @@ -0,0 +1,9 @@ +// @noImplicitAny: true + +let x: (a: string) => string; +let y = true; + +x = y && (a => { + const b: number = a; + return b; +}); \ No newline at end of file diff --git a/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd03.ts b/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd03.ts new file mode 100644 index 0000000000..1fba96c048 --- /dev/null +++ b/tests/cases/conformance/types/contextualTypes/logicalAnd/contextuallyTypeLogicalAnd03.ts @@ -0,0 +1,6 @@ +// @noImplicitAny: true + +let x: (a: string) => string; +let y = true; + +x = (a => a) && (b => b); \ No newline at end of file