TypeScript error

TS8016: An as cast in a .js file

  • Error TS8016
  • Tested with TypeScript 5.9

error TS8016: Type assertion expressions can only be used in TypeScript files. is reported for expr as Type in a .js file, and TS8037 for expr satisfies Type. Both are TypeScript operators; in JavaScript they are syntax errors.

JSDoc has an equivalent for each. A cast is a @type comment directly before a parenthesized expression: /** @type {HTMLInputElement} */ (document.querySelector("#quantity")). @satisfies is a tag on the declaration and checks the value without changing its type.

At a glance

CodeTS8016
MessageType assertion expressions can only be used in TypeScript files.
Also on this page: TS8037 Type satisfaction expressions can only be used in TypeScript files.
TypeScript's categoryerror
Listed underJavaScript 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 messages are TypeScript's own text from its diagnostic messages file (Apache-2.0).

What it looks like

settings.js Error confirmed 5.9.3 TypeScript 5.9.3, run 21 October 2026: settings.js(4,57): error TS8016: Type assertion expressions can only be used in TypeScript files. settings.js(7,67): error TS8037: Type satisfaction expressions can only be used in TypeScript files. 6.0.3 TypeScript 6.0.3, run 21 October 2026: the same as 5.9.3. settings.js(4,57): error TS8016: Type assertion expressions can only be used in TypeScript files. settings.js(7,67): error TS8037: Type satisfaction expressions can only be used in TypeScript files. 7.0.2 TypeScript 7.0.2, run 21 October 2026: the same as 5.9.3. settings.js(4,57): error TS8016: Type assertion expressions can only be used in TypeScript files. settings.js(7,67): error TS8037: Type satisfaction expressions can only be used in TypeScript files. checked 21 October 2026
/** @typedef {{ port: number, host: string }} ServerConfig */

const quantity = document.querySelector("#quantity") as HTMLInputElement;error TS8016: Type assertion expressions can only be used in TypeScript files.

export const config = { port: 3000, host: "localhost" } satisfies ServerConfig;error TS8037: Type satisfaction expressions can only be used in TypeScript files.

export const amount = quantity.valueAsNumber;
as and satisfies in a .js file.
jsconfig.json for this example
{
  "compilerOptions": {
    "checkJs": true,
    "noEmit": true,
    "strict": true,
    "target": "es2022",
    "module": "nodenext",
    "types": []
  },
  "include": ["*.js"]
}

What causes it

  • TypeScript code with as or satisfies was pasted into a .js file.

How to fix it

Use a JSDoc cast and @satisfies

The parentheses are required: /** @type {T} */ (value) casts, while a @type comment without them annotates the declaration instead. /** @satisfies {ServerConfig} */ above export const config = { ... } checks the object against the type and keeps its own, narrower type, exactly as satisfies does.

settings.js settings.ts TypeScript 5.9.3 TypeScript 5.9.3, run 21 October 2026: No errors. 6.0.3 TypeScript 6.0.3, run 21 October 2026: the same as 5.9.3. No errors. 7.0.2 TypeScript 7.0.2, run 21 October 2026: the same as 5.9.3. No errors. checked 21 October 2026
/** @typedef {{ port: number, host: string }} ServerConfig */

const quantity = /** @type {HTMLInputElement} */ (document.querySelector("#quantity"));

/** @satisfies {ServerConfig} */
export const config = { port: 3000, host: "localhost" };

export const amount = quantity.valueAsNumber;
type ServerConfig = { port: number; host: string };

const quantity = document.querySelector("#quantity") as HTMLInputElement;

export const config = { port: 3000, host: "localhost" } satisfies ServerConfig;

export const amount = quantity.valueAsNumber;
A JSDoc cast and @satisfies.
jsconfig.json for this example
{
  "compilerOptions": {
    "checkJs": true,
    "noEmit": true,
    "strict": true,
    "target": "es2022",
    "module": "nodenext",
    "types": []
  },
  "include": ["*.js"]
}

Gotchas

  • The older angle-bracket assertion (<HTMLElement>value) is read as JSX in a .js file and produces JSX errors instead of TS8016; it has no JavaScript form either, so use the JSDoc cast.
  • as const has a JSDoc form too: /** @type {const} */ ([10, 20]).

Sources

Last reviewed 20 October 2026.