Registry / serialization / typescript-react-intl

typescript-react-intl

JSON →
library0.4.1jsnpmunverified

typescript-react-intl is a utility library designed to extract string messages from TypeScript React components and other .ts files that leverage the React Intl library. It specifically targets messages defined using `<FormattedMessage/>` components and `defineMessages` function calls. The current stable version is 0.4.1. This package does not specify a rigid release cadence but operates with a 0.x versioning scheme, suggesting ongoing development or maintenance rather than fixed cycles. A key differentiator is its explicit support for TypeScript files and its ability to handle custom React components via a configurable `tagNames` option. It's also noted for its compatibility with React Native projects. It's important to note its limitation: it does not support `<FormattedHtmlMessage/>`.

npm install typescript-react-intl
INSTALL
IMPORT
SIG · TYPESCRIPT-REACT-I
T
typescript-react-intl
serializationjavascriptv0.4.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import parse from 'typescript-react-intl';
import { parse } from 'typescript-react-intl'; const parse = require('typescript-react-intl').parse;
The primary parsing function is exported as the default export of the package.
Options
import type { Options } from 'typescript-react-intl';
import { Options } from 'typescript-react-intl';
Type definition for the optional configuration object passed to the `parse` function, primarily used for specifying `tagNames` for custom components.
MessageDescriptor
import type { MessageDescriptor } from 'typescript-react-intl';
import { MessageDescriptor } from 'typescript-react-intl';
Type definition for the objects returned by the `parse` function, representing individual extracted React Intl messages with `id`, `defaultMessage`, and `description`.

Demonstrates how to use `typescript-react-intl` with `node-glob` and `fs` to find all TypeScript files, extract React Intl messages (including from custom components), and compile them into a locale-specific TypeScript module.

import { readFileSync, writeFileSync } from 'fs'; import { glob } from 'glob'; // Install 'glob' as a dev dependency: npm i glob -D import parse, { type MessageDescriptor, type Options } from 'typescript-react-intl'; /** * Extracts React Intl messages from TypeScript files and compiles them into a locale JSON. * @param pattern Glob pattern to find TypeScript files (e.g., 'src/**/*.@(tsx|ts)'). */ async function extractAndSaveMessages(pattern: string = 'src/**/*.@(tsx|ts)') { let allMessages: MessageDescriptor[] = []; try { const files = await glob(pattern); console.log(`Found ${files.length} files matching pattern: ${pattern}`); for (const f of files) { const contents = readFileSync(f, 'utf8'); // Example with custom components support, configure as needed. const parseOptions: Options = { tagNames: ["CustomFormattedText", "LocalizedDiv"], // Add your custom component names here }; const extracted = parse(contents, parseOptions); allMessages = allMessages.concat(extracted); } const locale: Record<string, string | undefined> = {}; allMessages.forEach((r) => { if (r.id) { locale[r.id] = r.defaultMessage; // Use defaultMessage as the locale value } }); const locales = { en: locale }; // Compile for 'en' locale const outputPath = `src/translations/all.ts`; writeFileSync( outputPath, `export default ${JSON.stringify(locales, null, 2)} \n`, 'utf8' ); console.log(`Successfully extracted ${allMessages.length} messages and saved to ${outputPath}`); } catch (err: any) { console.error(`Error during message extraction: ${err.message}`); process.exit(1); } } // Execute the extraction process extractAndSaveMessages().catch(console.error);
Debug
Known issues
breakingCustom component support was introduced in version `0.3.0`. Projects upgrading from versions prior to `0.3.0` that rely on custom components for message formatting will need to update their parsing configuration with the `tagNames` option.
fix
Upgrade to `typescript-react-intl@^0.3.0` and configure the `tagNames` option in the parser if you use custom components that wrap React Intl's message components.
affects: <0.3.0
gotchaThis library explicitly supports only `<FormattedMessage/>` and `defineMessages` for message extraction. It does NOT support `<FormattedHtmlMessage/>`, meaning messages defined using this component will be ignored.
fix
Ensure all messages intended for extraction are defined using `<FormattedMessage/>` or `defineMessages`. Avoid `<FormattedHtmlMessage/>` if you rely on this package for message extraction.
affects: >=0.1.0
gotchaThe `parse` function expects the full file contents as a string, not a file path. Users often mistakenly pass file paths directly, leading to parsing errors.
fix
Before calling `parse`, read the file content into a string using `fs.readFileSync(filePath, 'utf8')` or similar methods, then pass the string content to `parse`.
affects: >=0.1.0
gotchaAs a `0.x.x` version package, the API might not be fully stable, and minor versions could introduce breaking changes or significant behavioral shifts without strict adherence to semantic versioning.
fix
Pin to exact versions in `package.json` (e.g., `"typescript-react-intl": "0.4.1"`) to prevent unexpected updates. Review release notes carefully when upgrading to new minor versions.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: (0 , _typescriptReactIntl.default) is not a function
Attempting to use `require` with named import syntax (`.parse`) instead of accessing the default export.
fix
If using CommonJS `require`, use `const parse = require('typescript-react-intl').default;`. For ESM `import`, use `import parse from 'typescript-react-intl';`.
Error: Cannot find module 'typescript-react-intl'
The package is not installed or incorrectly referenced in `package.json`.
fix
Install the package using `npm install typescript-react-intl` or `yarn add typescript-react-intl`. Ensure it's correctly listed in your `package.json` dependencies.
No messages extracted from custom React components (e.g., `<MyText/>`)
The `tagNames` option was not configured or does not include the names of your custom components.
fix
Pass an `Options` object to the `parse` function, including an array of strings in the `tagNames` property with the names of your custom components (e.g., `parse(contents, { tagNames: ['MyText', 'StyledMessage'] })`).
Upgrade
Version history
0.4.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
typescript-react-intl — npm install typescript-react-intl · libregistry