Merge pull request #9170 from Microsoft/emptyTupleAssertions
Correctly check types in type assertions
This commit is contained in:
commit
9a2cf115c0
14 changed files with 112 additions and 2 deletions
|
@ -11524,7 +11524,10 @@ namespace ts {
|
|||
|
||||
function checkAssertion(node: AssertionExpression) {
|
||||
const exprType = getRegularTypeOfObjectLiteral(checkExpression(node.expression));
|
||||
|
||||
checkSourceElement(node.type);
|
||||
const targetType = getTypeFromTypeNode(node.type);
|
||||
|
||||
if (produceDiagnostics && targetType !== unknownType) {
|
||||
const widenedType = getWidenedType(exprType);
|
||||
if (!isTypeComparableTo(targetType, widenedType)) {
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
tests/cases/compiler/defaultValueInFunctionTypes.ts(1,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
|
||||
tests/cases/compiler/defaultValueInFunctionTypes.ts(2,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
|
||||
|
||||
|
||||
==== tests/cases/compiler/defaultValueInFunctionTypes.ts (1 errors) ====
|
||||
==== tests/cases/compiler/defaultValueInFunctionTypes.ts (2 errors) ====
|
||||
var x: (a: number = 1) => number;
|
||||
~~~~~~~~~~~~~
|
||||
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
|
||||
var y = <(a : string = "") => any>(undefined)
|
||||
var y = <(a : string = "") => any>(undefined)
|
||||
~~~~~~~~~~~~~~~
|
||||
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
|
|
@ -0,0 +1,11 @@
|
|||
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts(2,11): error TS2300: Duplicate identifier 'a'.
|
||||
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts(2,22): error TS2300: Duplicate identifier 'a'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions01.ts (2 errors) ====
|
||||
|
||||
let x = <{a: number; a: number}>{};
|
||||
~
|
||||
!!! error TS2300: Duplicate identifier 'a'.
|
||||
~
|
||||
!!! error TS2300: Duplicate identifier 'a'.
|
|
@ -0,0 +1,12 @@
|
|||
//// [duplicatePropertiesInTypeAssertions01.ts]
|
||||
|
||||
let x = <{a: number; a: number}>{};
|
||||
|
||||
//// [duplicatePropertiesInTypeAssertions01.js]
|
||||
var x = {};
|
||||
|
||||
|
||||
//// [duplicatePropertiesInTypeAssertions01.d.ts]
|
||||
declare let x: {
|
||||
a: number;
|
||||
};
|
|
@ -0,0 +1,11 @@
|
|||
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts(2,16): error TS2300: Duplicate identifier 'a'.
|
||||
tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts(2,27): error TS2300: Duplicate identifier 'a'.
|
||||
|
||||
|
||||
==== tests/cases/conformance/expressions/typeAssertions/duplicatePropertiesInTypeAssertions02.ts (2 errors) ====
|
||||
|
||||
let x = {} as {a: number; a: number};
|
||||
~
|
||||
!!! error TS2300: Duplicate identifier 'a'.
|
||||
~
|
||||
!!! error TS2300: Duplicate identifier 'a'.
|
|
@ -0,0 +1,12 @@
|
|||
//// [duplicatePropertiesInTypeAssertions02.ts]
|
||||
|
||||
let x = {} as {a: number; a: number};
|
||||
|
||||
//// [duplicatePropertiesInTypeAssertions02.js]
|
||||
var x = {};
|
||||
|
||||
|
||||
//// [duplicatePropertiesInTypeAssertions02.d.ts]
|
||||
declare let x: {
|
||||
a: number;
|
||||
};
|
|
@ -0,0 +1,9 @@
|
|||
tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts(2,10): error TS1122: A tuple type element list cannot be empty.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion01.ts (1 errors) ====
|
||||
|
||||
let x = <[]>[];
|
||||
~~
|
||||
!!! error TS1122: A tuple type element list cannot be empty.
|
||||
let y = x[0];
|
13
tests/baselines/reference/emptyTuplesTypeAssertion01.js
Normal file
13
tests/baselines/reference/emptyTuplesTypeAssertion01.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
//// [emptyTuplesTypeAssertion01.ts]
|
||||
|
||||
let x = <[]>[];
|
||||
let y = x[0];
|
||||
|
||||
//// [emptyTuplesTypeAssertion01.js]
|
||||
var x = [];
|
||||
var y = x[0];
|
||||
|
||||
|
||||
//// [emptyTuplesTypeAssertion01.d.ts]
|
||||
declare let x: [];
|
||||
declare let y: never;
|
|
@ -0,0 +1,9 @@
|
|||
tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts(2,15): error TS1122: A tuple type element list cannot be empty.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/tuple/emptyTuples/emptyTuplesTypeAssertion02.ts (1 errors) ====
|
||||
|
||||
let x = [] as [];
|
||||
~~
|
||||
!!! error TS1122: A tuple type element list cannot be empty.
|
||||
let y = x[0];
|
13
tests/baselines/reference/emptyTuplesTypeAssertion02.js
Normal file
13
tests/baselines/reference/emptyTuplesTypeAssertion02.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
//// [emptyTuplesTypeAssertion02.ts]
|
||||
|
||||
let x = [] as [];
|
||||
let y = x[0];
|
||||
|
||||
//// [emptyTuplesTypeAssertion02.js]
|
||||
var x = [];
|
||||
var y = x[0];
|
||||
|
||||
|
||||
//// [emptyTuplesTypeAssertion02.d.ts]
|
||||
declare let x: [];
|
||||
declare let y: never;
|
|
@ -0,0 +1,3 @@
|
|||
// @declaration: true
|
||||
|
||||
let x = <{a: number; a: number}>{};
|
|
@ -0,0 +1,3 @@
|
|||
// @declaration: true
|
||||
|
||||
let x = {} as {a: number; a: number};
|
|
@ -0,0 +1,4 @@
|
|||
// @declaration: true
|
||||
|
||||
let x = <[]>[];
|
||||
let y = x[0];
|
|
@ -0,0 +1,4 @@
|
|||
// @declaration: true
|
||||
|
||||
let x = [] as [];
|
||||
let y = x[0];
|
Loading…
Reference in a new issue