Skip to content

Commit 9a2cf11

Browse files
Merge pull request #9170 from Microsoft/emptyTupleAssertions
Correctly check types in type assertions
2 parents a1e0504 + 74a784c commit 9a2cf11

14 files changed

+112
-2
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11524,7 +11524,10 @@ namespace ts {
1152411524

1152511525
function checkAssertion(node: AssertionExpression) {
1152611526
const exprType = getRegularTypeOfObjectLiteral(checkExpression(node.expression));
11527+
11528+
checkSourceElement(node.type);
1152711529
const targetType = getTypeFromTypeNode(node.type);
11530+
1152811531
if (produceDiagnostics && targetType !== unknownType) {
1152911532
const widenedType = getWidenedType(exprType);
1153011533
if (!isTypeComparableTo(targetType, widenedType)) {
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
tests/cases/compiler/defaultValueInFunctionTypes.ts(1,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
2+
tests/cases/compiler/defaultValueInFunctionTypes.ts(2,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
23

34

4-
==== tests/cases/compiler/defaultValueInFunctionTypes.ts (1 errors) ====
5+
==== tests/cases/compiler/defaultValueInFunctionTypes.ts (2 errors) ====
56
var x: (a: number = 1) => number;
67
~~~~~~~~~~~~~
78
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
8-
var y = <(a : string = "") => any>(undefined)
9+
var y = <(a : string = "") => any>(undefined)
10+
~~~~~~~~~~~~~~~
11+
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts(2,11): error TS2300: Duplicate identifier 'a'.
2+
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts(2,22): error TS2300: Duplicate identifier 'a'.
3+
4+
5+
==== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts (2 errors) ====
6+
7+
let x = <{a: number; a: number}>{};
8+
~
9+
!!! error TS2300: Duplicate identifier 'a'.
10+
~
11+
!!! error TS2300: Duplicate identifier 'a'.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [duplicatePropertiesInTypeAssertions01.ts]
2+
3+
let x = <{a: number; a: number}>{};
4+
5+
//// [duplicatePropertiesInTypeAssertions01.js]
6+
var x = {};
7+
8+
9+
//// [duplicatePropertiesInTypeAssertions01.d.ts]
10+
declare let x: {
11+
a: number;
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts(2,16): error TS2300: Duplicate identifier 'a'.
2+
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts(2,27): error TS2300: Duplicate identifier 'a'.
3+
4+
5+
==== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts (2 errors) ====
6+
7+
let x = {} as {a: number; a: number};
8+
~
9+
!!! error TS2300: Duplicate identifier 'a'.
10+
~
11+
!!! error TS2300: Duplicate identifier 'a'.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [duplicatePropertiesInTypeAssertions02.ts]
2+
3+
let x = {} as {a: number; a: number};
4+
5+
//// [duplicatePropertiesInTypeAssertions02.js]
6+
var x = {};
7+
8+
9+
//// [duplicatePropertiesInTypeAssertions02.d.ts]
10+
declare let x: {
11+
a: number;
12+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts(2,10): error TS1122: A tuple type element list cannot be empty.
2+
3+
4+
==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts (1 errors) ====
5+
6+
let x = <[]>[];
7+
~~
8+
!!! error TS1122: A tuple type element list cannot be empty.
9+
let y = x[0];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [emptyTuplesTypeAssertion01.ts]
2+
3+
let x = <[]>[];
4+
let y = x[0];
5+
6+
//// [emptyTuplesTypeAssertion01.js]
7+
var x = [];
8+
var y = x[0];
9+
10+
11+
//// [emptyTuplesTypeAssertion01.d.ts]
12+
declare let x: [];
13+
declare let y: never;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts(2,15): error TS1122: A tuple type element list cannot be empty.
2+
3+
4+
==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts (1 errors) ====
5+
6+
let x = [] as [];
7+
~~
8+
!!! error TS1122: A tuple type element list cannot be empty.
9+
let y = x[0];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [emptyTuplesTypeAssertion02.ts]
2+
3+
let x = [] as [];
4+
let y = x[0];
5+
6+
//// [emptyTuplesTypeAssertion02.js]
7+
var x = [];
8+
var y = x[0];
9+
10+
11+
//// [emptyTuplesTypeAssertion02.d.ts]
12+
declare let x: [];
13+
declare let y: never;

0 commit comments

Comments
 (0)