Merge pull request #6355 from Microsoft/contextuallyTypeAndAndComma
Contextually type '&&' and the comma operator
This commit is contained in:
commit
fec691ec22
21 changed files with 237 additions and 0 deletions
|
@ -7362,6 +7362,12 @@ namespace ts {
|
||||||
}
|
}
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
else if (operator === SyntaxKind.AmpersandAmpersandToken || operator === SyntaxKind.CommaToken) {
|
||||||
|
if (node === binaryExpression.right) {
|
||||||
|
return getContextualType(binaryExpression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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; });
|
|
@ -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))
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
});
|
15
tests/baselines/reference/contextuallyTypeCommaOperator02.js
Normal file
15
tests/baselines/reference/contextuallyTypeCommaOperator02.js
Normal file
|
@ -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;
|
||||||
|
});
|
|
@ -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.
|
|
@ -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; });
|
11
tests/baselines/reference/contextuallyTypeLogicalAnd01.js
Normal file
11
tests/baselines/reference/contextuallyTypeLogicalAnd01.js
Normal file
|
@ -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; });
|
|
@ -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))
|
||||||
|
|
20
tests/baselines/reference/contextuallyTypeLogicalAnd01.types
Normal file
20
tests/baselines/reference/contextuallyTypeLogicalAnd01.types
Normal file
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
});
|
17
tests/baselines/reference/contextuallyTypeLogicalAnd02.js
Normal file
17
tests/baselines/reference/contextuallyTypeLogicalAnd02.js
Normal file
|
@ -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;
|
||||||
|
});
|
|
@ -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.
|
11
tests/baselines/reference/contextuallyTypeLogicalAnd03.js
Normal file
11
tests/baselines/reference/contextuallyTypeLogicalAnd03.js
Normal file
|
@ -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; });
|
|
@ -0,0 +1,5 @@
|
||||||
|
// @noImplicitAny: true
|
||||||
|
|
||||||
|
let x: (a: string) => string;
|
||||||
|
|
||||||
|
x = (100, a => a);
|
|
@ -0,0 +1,8 @@
|
||||||
|
// @noImplicitAny: true
|
||||||
|
|
||||||
|
let x: (a: string) => string;
|
||||||
|
|
||||||
|
x = (100, a => {
|
||||||
|
const b: number = a;
|
||||||
|
return b;
|
||||||
|
});
|
|
@ -0,0 +1,5 @@
|
||||||
|
// @noImplicitAny: true
|
||||||
|
|
||||||
|
let x: (a: string) => string;
|
||||||
|
|
||||||
|
x = (a => a, b => b);
|
|
@ -0,0 +1,6 @@
|
||||||
|
// @noImplicitAny: true
|
||||||
|
|
||||||
|
let x: (a: string) => string;
|
||||||
|
let y = true;
|
||||||
|
|
||||||
|
x = y && (a => a);
|
|
@ -0,0 +1,9 @@
|
||||||
|
// @noImplicitAny: true
|
||||||
|
|
||||||
|
let x: (a: string) => string;
|
||||||
|
let y = true;
|
||||||
|
|
||||||
|
x = y && (a => {
|
||||||
|
const b: number = a;
|
||||||
|
return b;
|
||||||
|
});
|
|
@ -0,0 +1,6 @@
|
||||||
|
// @noImplicitAny: true
|
||||||
|
|
||||||
|
let x: (a: string) => string;
|
||||||
|
let y = true;
|
||||||
|
|
||||||
|
x = (a => a) && (b => b);
|
Loading…
Reference in a new issue