Skip to content

Commit

Permalink
rename option, improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn committed Nov 5, 2024
1 parent 4e990ef commit aadd7aa
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
14 changes: 8 additions & 6 deletions docs/src/pages/docs/workflows/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ function UserPage({user}) {

TypeScript currently has a [limitation](https://github.com/microsoft/TypeScript/issues/32063) where it infers the types of an imported JSON module as rather broad.

Due to this, `next-intl` provides a stopgap solution that allows you to compile an accompanying `.d.json.ts` file for the messages that you're assigning to your `AppConfig`.
Due to this, `next-intl` provides a stopgap solution that allows you to generate an accompanying `.d.json.ts` file for the messages that you're assigning to your `AppConfig`.

**Usage:**

1. Enable the `compileMessagesDeclaration` setting in your Next.js config:
1. Enable the `createMessagesDeclaration` setting in your Next.js config:

```tsx filename="next.config.mjs"
import {createNextIntlPlugin} from 'next-intl/plugin';

const withNextIntl = createNextIntlPlugin({
experimental: {
// Use the path to the messages that you're using in `AppConfig`
compileMessagesDeclaration: './messages/en.json'
createMessagesDeclaration: './messages/en.json'
}
});

Expand All @@ -118,21 +118,23 @@ const withNextIntl = createNextIntlPlugin({
}
```

3. Upvote [microsoft/TypeScript#32063](https://github.com/microsoft/TypeScript/issues/32063) so that we can remove this workaround in the future.

With this setup in place, you'll see a new declaration file generated in your `messages` directory once you run `next dev` or `next build`:

```diff
messages/en.json
+ messages/en.d.json.ts
```

You can choose to ignore the generated declaration file in Git:
This declaration file will provide the exact types for the messages that you're using in `AppConfig`, enabling type safety for message arguments.

You can choose to ignore this file in Git:

```text filename=".gitignore"
messages/*.d.json.ts
```

Please consider upvoting [microsoft/TypeScript#32063](https://github.com/microsoft/TypeScript/issues/32063) so that we can hopefully remove this workaround in the future.

## `Formats`

If you're using [global formats](/docs/usage/configuration#formats), you can strictly type the format names that are referenced in calls to `format.dateTime`, `format.number` and `format.list`.
Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-router-playground/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import createNextIntlPlugin from 'next-intl/plugin';
const withNextIntl = createNextIntlPlugin({
requestConfig: './src/i18n/request.tsx',
experimental: {
compileMessagesDeclaration: './messages/en.json'
createMessagesDeclaration: './messages/en.json'
}
});
const withMdx = mdxPlugin();
Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-router/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import createNextIntlPlugin from 'next-intl/plugin';

const withNextIntl = createNextIntlPlugin({
experimental: {
compileMessagesDeclaration: './messages/en.json'
createMessagesDeclaration: './messages/en.json'
}
});

Expand Down
6 changes: 3 additions & 3 deletions packages/next-intl/src/plugin/compileMessagesDeclaration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ function runOnce(fn: () => void) {
fn();
}

export default function compileMessagesDeclaration(messagesPath: string) {
export default function createMessagesDeclaration(messagesPath: string) {
const fullPath = path.resolve(messagesPath);

if (!fs.existsSync(fullPath)) {
throwError(
`\`compileMessagesDeclaration\` points to a non-existent file: ${fullPath}`
`\`createMessagesDeclaration\` points to a non-existent file: ${fullPath}`
);
}
if (!fullPath.endsWith('.json')) {
throwError(
`\`compileMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`
`\`createMessagesDeclaration\` needs to point to a JSON file. Received: ${fullPath}`
);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/next-intl/src/plugin/createNextIntlPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {NextConfig} from 'next';
import compileMessagesDeclaration from './compileMessagesDeclaration.tsx';
import createMessagesDeclaration from './createMessagesDeclaration.tsx';
import getNextConfig from './getNextConfig.tsx';
import type {PluginConfig} from './types.tsx';
import {warn} from './utils.tsx';
Expand All @@ -14,9 +14,9 @@ function initPlugin(
);
}

if (pluginConfig.experimental?.compileMessagesDeclaration) {
compileMessagesDeclaration(
pluginConfig.experimental.compileMessagesDeclaration
if (pluginConfig.experimental?.createMessagesDeclaration) {
createMessagesDeclaration(
pluginConfig.experimental.createMessagesDeclaration
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next-intl/src/plugin/types.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type PluginConfig = {
requestConfig?: string;
experimental?: {
compileMessagesDeclaration?: string;
createMessagesDeclaration?: string;
};
};

0 comments on commit aadd7aa

Please sign in to comment.