Skip to content

Commit 2efaa7c

Browse files
author
Andy
authored
Forbid non-null assertion in '.js' files (#17481)
1 parent 58769e1 commit 2efaa7c

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16384,6 +16384,10 @@ namespace ts {
1638416384
}
1638516385

1638616386
function checkNonNullAssertion(node: NonNullExpression) {
16387+
if (isInJavaScriptFile(node)) {
16388+
error(node, Diagnostics.non_null_assertions_can_only_be_used_in_a_ts_file);
16389+
}
16390+
1638716391
return getNonNullableType(checkExpression(node.expression));
1638816392
}
1638916393

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,6 +3459,10 @@
34593459
"category": "Error",
34603460
"code": 8012
34613461
},
3462+
"'non-null assertions' can only be used in a .ts file.": {
3463+
"category": "Error",
3464+
"code": 8013
3465+
},
34623466
"'enum declarations' can only be used in a .ts file.": {
34633467
"category": "Error",
34643468
"code": 8015
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/src/a.js(1,1): error TS8013: 'non-null assertions' can only be used in a .ts file.
2+
3+
4+
==== /src/a.js (1 errors) ====
5+
0!
6+
~~
7+
!!! error TS8013: 'non-null assertions' can only be used in a .ts file.
8+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [a.js]
2+
0!
3+
4+
5+
//// [a.js]
6+
0;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @filename: /src/a.js
4+
// @out: /bin/a.js
5+
0!

0 commit comments

Comments
 (0)