Skip to content

Commit

Permalink
🐛 fix: 修正类型无法自动提示的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jul 3, 2021
1 parent 58cdd2b commit 189f722
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 4 additions & 2 deletions packages/i18n/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import type { LocaleMessageMaps, PrimitiveType } from './types';
/**
*
*/
const createI18n = (messageObj: LocaleMessageMaps) => {
const createI18n = <T extends Record<string, string>>(
messageObj: LocaleMessageMaps<T>,
) => {
const IntlProvider: FC = ({ children }) => {
const { locale, messages } = useLocaleMessages(messageObj);
return (
Expand All @@ -22,7 +24,7 @@ const createI18n = (messageObj: LocaleMessageMaps) => {
};

const useFormatMessage = (): ((
id: keyof LocaleMessageMaps['zh-CN'],
id: keyof T,
values?: Record<string, PrimitiveType>,
) => string) => {
const intl = useIntl();
Expand Down
8 changes: 3 additions & 5 deletions packages/i18n/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export type LocalesType = 'en-US' | 'zh-CN';

export type MessageObj = Record<string, string>;

export type LocaleMessageMaps = {
'zh-CN': MessageObj;
'en-US'?: MessageObj;
export type LocaleMessageMaps<T = Record<string, string>> = {
'zh-CN': T;
'en-US'?: T;
};

export type { PrimitiveType } from 'intl-messageformat';
5 changes: 3 additions & 2 deletions packages/i18n/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { act, findByTestId, render } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';

import createI18n from '@arvinxu/i18n';
import type { LocaleMessageMaps } from '@arvinxu/i18n';

const zhCN = {
hello: '你好',
good: '好',
};
const en = {
hello: 'hello',
good: 'good',
};
const locales: LocaleMessageMaps = {
const locales = {
'zh-CN': zhCN,
'en-US': en,
};
Expand Down

1 comment on commit 189f722

@vercel
Copy link

@vercel vercel bot commented on 189f722 Jul 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.