- Error
- Reported by the lexer and parser
TS8006
'{0}' declarations can only be used in TypeScript files.
An `interface`, `enum`, `namespace` or `module` declaration appears in a JavaScript file. Move it to a .ts file.
What it means
TypeScript-only declarations (interfaces, enums, namespaces, module declarations) can't appear in a .js file. The message names the kind of declaration.
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 at the same place instead.
Example
enum Color { Red, Green }
cargo run -p ts_r_parser --example dump -- index.js --ts-in-js report ts-r outputindex.js(1,6): error TS8006: 'enum' declarations 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
- Move the declaration to a
.tsor.d.tsfile and import it. - Replace an enum with a frozen object:
const Color = Object.freeze({ Red: 0, Green: 1 });. - Describe interfaces with JSDoc
@typedef.