- Error
- Reported by the lexer and parser
TS8010
Type annotations can only be used in TypeScript files.
A type annotation appears in a JavaScript file. Rename the file to .ts, or move the type into a JSDoc comment.
What it means
JavaScript files can't contain TypeScript syntax. A : Type annotation on a variable, parameter or return type in a .js file is reported here, at the type.
ts-r's parser reports this code when it reads .js files with tsc's rules for TypeScript syntax in JavaScript. ts-r-check doesn't report it yet: today it parses .js files with the plain JavaScript grammar and reports a syntax error (TS1005) at the same place.
Example
let total: number = 1;
cargo run -p ts_r_parser --example dump -- index.js --ts-in-js report ts-r outputindex.js(1,12): error TS8010: Type annotations can only be used in TypeScript files.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Rename the file to
.ts(or.mts/.cts) if it's meant to be TypeScript. - Keep it JavaScript and write the type as JSDoc:
/** @type {number} */ let total = 1;.