formatjs extract typescript error on message id
Covers formatjs extract dying on TypeScript syntax: add preset-typescript to the babel config and re-run. Use it when extraction throws a TS syntax error. Not for empty extraction results or plain JS parse failures.
Fix formatjs extract failing on TypeScript message ids
TL;DR
The extractor is running without TypeScript support, so add the TS preset to the babel config it uses. Formatjs extract shells out to babel, and plain babel chokes on types, enums, and satisfies expressions. Once the TS plugin is in the chain, extraction sees the messages again.
The error
Error: Support for the experimental syntax 'typescript' isn't currently enabled
while running: formatjs extract 'src/**/*.ts'Fix it
Step 1: Check what babel config the extractor picks up
ls babel.config.js .babelrc; grep -rn "preset-typescript" babel.config.jsExpected: You see whether the TS preset is present or missing.
Step 2: Add the TypeScript preset
node -e "console.log('in babel.config.js presets add: [\"@babel/preset-typescript\", {\"isTSX\": true, \"allExtensions\": true}]')"Expected: The config now includes preset-typescript.
Step 3: Re-run extraction on the TS sources
npx formatjs extract 'src/**/*.ts*' --out-file /tmp/messages.json | tail -3Expected: Extraction completes and the output file has your messages.
Step 4: Confirm message count looks right
node -e "const m=require('/tmp/messages.json'); console.log(Object.keys(m).length,'messages extracted')"Expected: The count matches roughly what you expect from the codebase.
When to use this
- formatjs extract fails with a TypeScript syntax error
- You extract from .ts or .tsx files
When NOT to use this
- Extraction runs but finds zero messages, check the glob and the defineMessages usage
- The failure is on a .js file, that is a different parser issue
Tool and version compatibility
- @formatjs/cli v5/v6, @babel/preset-typescript
- Babel 7 with a babel.config.js at the repo root
Variant phrasings
extract fails on TS enums in message files
Same fix. The TS preset handles enums, the default parser does not.
works locally but fails in CI
CI probably runs extract from a different cwd so babel finds no config. Pass --config or run from the repo root.
Why it happens
Formatjs extract parses sources with babel using your project babel config. Without preset-typescript, babel treats .ts files as plain JS and dies on the first type annotation. The extractor never gets to the defineMessages calls.
Edge cases
- isTSX and allExtensions matter, without them .ts files may still fail
- A .babelrc in a subfolder can shadow the root config for those files
- Decorators need their own plugin too if message files use them
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_ujNWV1oCgPhjNBrZLArt0Q