TypeScript error

TS8010: TypeScript annotations in a .js file

  • Error TS8010
  • Tested with TypeScript 5.9

error TS8010: Type annotations can only be used in TypeScript files. is reported for every : Type annotation in a .js file: on parameters, return types, variables and class fields. TS8004 is the same for a type parameter list (function first<T>(...)). The code would also fail to run, because JavaScript has no such syntax; Node.js's type stripping only applies to .ts files.

This is what happens when code is copied from a TypeScript project or an answer written for one. Every annotation has a JSDoc spelling, and the checker treats them the same way: @param and @returns for functions, @type for variables, @template for type parameters.

At a glance

CodeTS8010
MessageType annotations can only be used in TypeScript files.
Also on this page: TS8004 Type parameter declarations 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

cart.js Error confirmed 5.9.3 TypeScript 5.9.3, run 21 October 2026: cart.js(2,35): error TS8010: Type annotations can only be used in TypeScript files. cart.js(2,46): error TS8010: Type annotations can only be used in TypeScript files. cart.js(7,23): error TS8010: Type annotations can only be used in TypeScript files. cart.js(10,23): error TS8004: Type parameter declarations can only be used in TypeScript files. cart.js(10,33): error TS8010: Type annotations can only be used in TypeScript files. cart.js(10,39): error TS8010: Type annotations can only be used in TypeScript files. 6.0.3 TypeScript 6.0.3, run 21 October 2026: the same as 5.9.3. cart.js(2,35): error TS8010: Type annotations can only be used in TypeScript files. cart.js(2,46): error TS8010: Type annotations can only be used in TypeScript files. cart.js(7,23): error TS8010: Type annotations can only be used in TypeScript files. cart.js(10,23): error TS8004: Type parameter declarations can only be used in TypeScript files. cart.js(10,33): error TS8010: Type annotations can only be used in TypeScript files. cart.js(10,39): error TS8010: Type annotations can only be used in TypeScript files. 7.0.2 TypeScript 7.0.2, run 21 October 2026: the same as 5.9.3. cart.js(2,35): error TS8010: Type annotations can only be used in TypeScript files. cart.js(2,46): error TS8010: Type annotations can only be used in TypeScript files. cart.js(7,23): error TS8010: Type annotations can only be used in TypeScript files. cart.js(10,23): error TS8004: Type parameter declarations can only be used in TypeScript files. cart.js(10,33): error TS8010: Type annotations can only be used in TypeScript files. cart.js(10,39): error TS8010: Type annotations can only be used in TypeScript files. checked 21 October 2026
export function cartTotal(prices: number[]): number {error TS8010: Type annotations can only be used in TypeScript files.error TS8010: Type annotations can only be used in TypeScript files.
  return prices.reduce((sum, price) => sum + price, 0);
}

export let itemCount: number = 0;error TS8010: Type annotations can only be used in TypeScript files.

export function first<T>(items: T[]): T | undefined {error TS8004: Type parameter declarations can only be used in TypeScript files.error TS8010: Type annotations can only be used in TypeScript files.error TS8010: Type annotations can only be used in TypeScript files.
  return items[0];
}
TypeScript annotations pasted into 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 was pasted into a .js file, or a .ts file was renamed to .js.
  • The editor is not misconfigured: it checks a .js file as JavaScript, as it should, so the TypeScript syntax is what has to change.

How to fix it

Write the same types in JSDoc

Move each annotation into a comment: @param {number[]} prices and @returns {number} above the function, /** @type {number} */ above the variable, @template T for the type parameter. The fixed example's TypeScript tab shows the original next to it.

cart.js cart.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
/**
 * @param {number[]} prices
 * @returns {number}
 */
export function cartTotal(prices) {
  return prices.reduce((sum, price) => sum + price, 0);
}

/** @type {number} */
export let itemCount = 0;

/**
 * @template T
 * @param {T[]} items
 * @returns {T | undefined}
 */
export function first(items) {
  return items[0];
}
export function cartTotal(prices: number[]): number {
  return prices.reduce((sum, price) => sum + price, 0);
}

export let itemCount: number = 0;

export function first<T>(items: T[]): T | undefined {
  return items[0];
}
The same types in JSDoc.
jsconfig.json for this example
{
  "compilerOptions": {
    "checkJs": true,
    "noEmit": true,
    "strict": true,
    "target": "es2022",
    "module": "nodenext",
    "types": []
  },
  "include": ["*.js"]
}

Or make the file TypeScript

If the project is meant to have .ts files, rename the file and make sure a tsconfig.json includes it. A JavaScript project that wants types without a build step keeps .js and JSDoc.

Gotchas

  • Other TypeScript-only syntax has its own codes in the same family: as is TS8016, ! is TS8013, interface and enum are TS8006, type X = is TS8008, private and readonly are TS8009, parameter properties are TS8012.
  • A definite assignment annotation (let total!: number) is TS8010 too; in JSDoc, initialize the variable instead.

Sources

Last reviewed 20 October 2026.