Skip to content

Commit

Permalink
feat: Add type exports to enable declaration: true in `tsconfig.jso…
Browse files Browse the repository at this point in the history
…n` (#1509)

Re: <#1014>

Fix the type issues encountered when re-exporting types in a client
application with `compilerOptions.declaration` set to `true`. This
change addresses errors such as:

> The inferred type of 'Link' cannot be named without a reference to
'../node_modules/next-intl/dist/types/src/shared/types'. This is likely
not portable. A type annotation is necessary.ts(2742)

---------

Co-authored-by: Jan Amann <[email protected]>
  • Loading branch information
osaton and amannn authored Nov 8, 2024
1 parent 9daf2ea commit 6b2ca9c
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Ensure that exported types function as expected for library creators.
//
// Most functionality is already tested through usage in this application.
// This file includes exports for any that are not yet covered.

import {
createFormatter,
createTranslator,
initializeConfig,
useFormatter,
useLocale,
useMessages,
useNow,
useTimeZone,
useTranslations
} from 'next-intl';
import createNextIntlPlugin from 'next-intl/plugin';
import {
getFormatter,
getLocale,
getMessages,
getNow,
getTimeZone,
getTranslations
} from 'next-intl/server';

export function useExports() {
const messages = useMessages();
const now = useNow();
const locale = useLocale();
const timezone = useTimeZone();
const formatter = useFormatter();
const translations = useTranslations();

return {
messages,
now,
locale,
timezone,
formatter,
translations
};
}

export async function asyncApis() {
const messages = await getMessages();
const now = await getNow();
const locale = await getLocale();
const timezone = await getTimeZone();
const formatter = await getFormatter();
const translations = await getTranslations();

return {
messages,
now,
locale,
timezone,
formatter,
translations
};
}

export const withNextIntl = createNextIntlPlugin();
export const config = initializeConfig({locale: 'en'});
export const translator = createTranslator({locale: 'en'});
export const formatter = createFormatter({
locale: 'en',
now: new Date(2022, 10, 6, 20, 20, 0, 0)
});
5 changes: 4 additions & 1 deletion examples/example-app-router-playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
],
"paths": {
"@/*": ["./src/*"]
}
},

// See https://github.com/amannn/next-intl/pull/1509
"declaration": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
Expand Down
2 changes: 2 additions & 0 deletions packages/next-intl/src/navigation/react-client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export {default as createSharedPathnamesNavigation} from './createSharedPathname
export {default as createLocalizedPathnamesNavigation} from './createLocalizedPathnamesNavigation';
export {default as createNavigation} from './createNavigation';

export type {QueryParams} from '../shared/utils';

import type {
Locales,
Pathnames as PathnamesDeprecatedExport
Expand Down
8 changes: 7 additions & 1 deletion packages/next-intl/src/routing/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export type {Pathnames, LocalePrefix, DomainsConfig} from './types';
export type {
Pathnames,
DomainsConfig,
LocalePrefix,
LocalePrefixMode
} from './types';
export type {RoutingConfig} from './config';
export {default as defineRouting} from './defineRouting';
6 changes: 5 additions & 1 deletion packages/next-intl/src/server/react-server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* Server-only APIs available via `next-intl/server`.
*/

export {default as getRequestConfig} from './getRequestConfig';
export {
default as getRequestConfig,
type GetRequestConfigParams,
type RequestConfig
} from './getRequestConfig';
export {default as getFormatter} from './getFormatter';
export {default as getNow} from './getNow';
export {default as getTimeZone} from './getTimeZone';
Expand Down
2 changes: 2 additions & 0 deletions packages/use-intl/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type {default as Formats} from './Formats';
export type {default as IntlConfig} from './IntlConfig';
export type {default as DateTimeFormatOptions} from './DateTimeFormatOptions';
export type {default as NumberFormatOptions} from './NumberFormatOptions';
export type {default as RelativeTimeFormatOptions} from './RelativeTimeFormatOptions';
export type {default as Timezone} from './TimeZone';
export {default as IntlError, IntlErrorCode} from './IntlError';
export {default as createTranslator} from './createTranslator';
export {default as createFormatter} from './createFormatter';
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
packages:
- "packages/*"
- "examples/*"
- "docs"
- 'packages/*'
- 'examples/*'
- 'docs'

0 comments on commit 6b2ca9c

Please sign in to comment.