diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4335b6fe2f0..212de3a2159 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16384,6 +16384,10 @@ namespace ts { } function checkNonNullAssertion(node: NonNullExpression) { + if (isInJavaScriptFile(node)) { + error(node, Diagnostics.non_null_assertions_can_only_be_used_in_a_ts_file); + } + return getNonNullableType(checkExpression(node.expression)); } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 1ffc78134c3..3b4b0ddf667 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3459,6 +3459,10 @@ "category": "Error", "code": 8012 }, + "'non-null assertions' can only be used in a .ts file.": { + "category": "Error", + "code": 8013 + }, "'enum declarations' can only be used in a .ts file.": { "category": "Error", "code": 8015 diff --git a/tests/baselines/reference/jsFileCompilationNonNullAssertion.errors.txt b/tests/baselines/reference/jsFileCompilationNonNullAssertion.errors.txt new file mode 100644 index 00000000000..13670601f70 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationNonNullAssertion.errors.txt @@ -0,0 +1,8 @@ +/src/a.js(1,1): error TS8013: 'non-null assertions' can only be used in a .ts file. + + +==== /src/a.js (1 errors) ==== + 0! + ~~ +!!! error TS8013: 'non-null assertions' can only be used in a .ts file. + \ No newline at end of file diff --git a/tests/baselines/reference/jsFileCompilationNonNullAssertion.js b/tests/baselines/reference/jsFileCompilationNonNullAssertion.js new file mode 100644 index 00000000000..3790ff66c17 --- /dev/null +++ b/tests/baselines/reference/jsFileCompilationNonNullAssertion.js @@ -0,0 +1,6 @@ +//// [a.js] +0! + + +//// [a.js] +0; diff --git a/tests/cases/compiler/jsFileCompilationNonNullAssertion.ts b/tests/cases/compiler/jsFileCompilationNonNullAssertion.ts new file mode 100644 index 00000000000..cbe21e91dc6 --- /dev/null +++ b/tests/cases/compiler/jsFileCompilationNonNullAssertion.ts @@ -0,0 +1,5 @@ +// @allowJs: true +// @checkJs: true +// @filename: /src/a.js +// @out: /bin/a.js +0!