Skip to content

Commit

Permalink
Add error for asserting non-nullness of already non-null expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheeo committed Jan 30, 2017
1 parent aeeff28 commit e6d1651
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13970,7 +13970,18 @@ namespace ts {
}

function checkNonNullAssertion(node: NonNullExpression) {
return getNonNullableType(checkExpression(node.expression));
if(!strictNullChecks) {
// Actually adding this error results in numerous errors compiling the ts codebase
// In the interest of keeping this PR small, it's been omitted
// error(node, Diagnostics.Use_of_non_null_assertion_operator_without_strictNullChecks_enabled);
return checkExpression(node.expression);
}
const expr = checkExpression(node.expression);
const nonNullable = getNonNullableType(expr);
if(isTypeIdenticalTo(nonNullable, expr)) {
error(node, Diagnostics.The_expression_asserted_to_be_non_null_is_already_non_null);
}
return nonNullable;
}

function checkMetaProperty(node: MetaProperty) {
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,14 @@
"category": "Error",
"code": 2704
},
"The expression asserted to be non-null is already non-null": {
"category": "Error",
"code": 2705
},
"Use of non-null assertion operator without strictNullChecks enabled": {
"category": "Error",
"code": 2706
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down

0 comments on commit e6d1651

Please sign in to comment.