error TS8013: Non-null assertions can only be used in TypeScript files. is reported for a postfix ! such as document.getElementById("app")! in a .js file. In TypeScript the ! tells the checker a value is not null or undefined; JavaScript has no such operator.
Without it, the original problem returns: the value may be null (TS2531 or TS18047). Either prove it is there with a check, which also helps when the page changes, or state it with a cast.
At a glance
| Code | TS8013 |
|---|---|
| Message | Non-null assertions can only be used in TypeScript files. |
| TypeScript's category | error |
| Listed under | JavaScript files only |
| Turned on by | No option: every checked file reports it |
| In .js files | Reported when the file is checked: checkJs in jsconfig.json, or // @ts-check at the top of the file |
The message is TypeScript's own text from its diagnostic messages file (Apache-2.0).
What it looks like
const root = document.getElementById("app")!;error TS8013: Non-null assertions can only be used in TypeScript files.
root.append("Ready");jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["*.js"]
} What causes it
- TypeScript code using
!was pasted into a.jsfile.
How to fix it
Check the value, or cast it
if (!root) throw new Error(...) narrows the variable and fails with a clear message if the element is ever missing. /** @type {HTMLElement} */ (document.querySelector("footer")) is the JSDoc version of the assertion, for elements the markup always has.
const root = document.getElementById("app");
if (!root) throw new Error("app.js: no #app element on this page");
root.append("Ready");
const footer = /** @type {HTMLElement} */ (document.querySelector("footer"));
footer.append("Loaded");jsconfig.json for this example
{
"compilerOptions": {
"checkJs": true,
"noEmit": true,
"strict": true,
"target": "es2022",
"module": "nodenext",
"types": []
},
"include": ["*.js"]
} Gotchas
!before a value (!root) is ordinary JavaScript negation; only the postfix form is TypeScript.- In JSDoc types, a leading
!({!HTMLElement}) is accepted but does not removenull, so it is no substitute; see the non-null assertions page.
Sources
- Everyday Types: Non-null Assertion Operator · TypeScript handbook
- JSDoc Reference: Casts · TypeScript handbook
- diagnosticMessages.json (TypeScript 5.9.3) · TypeScript on GitHub
Last reviewed 20 October 2026.