Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong typings of intl.get() #101

Open
zry656565 opened this issue Nov 16, 2018 · 4 comments
Open

Wrong typings of intl.get() #101

zry656565 opened this issue Nov 16, 2018 · 4 comments

Comments

@zry656565
Copy link

zry656565 commented Nov 16, 2018

https://github.com/alibaba/react-intl-universal/blob/master/typings/index.d.ts#L31-L4

shows intl.get() will return a string forever.

    /**
     * Get the formatted message by key
     * @param {string} key The string representing key in locale data file
     * @returns {string} message
     */
    export function get(key: string): string;

    /**
     * Get the formatted message by key
     * @param {string} key The string representing key in locale data file
     * @param {Object} variables Variables in message
     * @returns {string} message
     */
    export function get(key: string, value: any): string;

But here's a exception:

intl.init({
  currentLocale: 'zh',
  locales: {
    zh: {
      category: {
        dog: '狗',
        cat: '猫'
      },
    }
  }
});

It's fine to visit intl.get('categroy.dog') or intl.get('category.cat'), but when I visit intl.get('category'), it returns an object to me, which isn't synced to type definitions.

I think it's related to this line change, https://github.com/alibaba/react-intl-universal/blob/master/src/index.js#L113 , which returns an empty string in 1.11.6.

The actual type should be:

function get(key: string, value?: any): string | { [key:string]: string };

But this definition isn't so friendly to ts codes.

intl.get('category.dog').d('Dog');

will raise an error in this code in typescript. cause type of intl.get('category.dog') isn't a string now.

Suggestion

Make a breaking change to .get() api in next major version of react-intl-universal, let it accept an another argument onlyString: boolean.

new definition:

function get(key: string, onlyString?: true): string;
function get(key: string, onlyString?: false): string | { [key:string]: string };
function get(key: string, val?: { [key:string]: any }, onlyString?: true): string;
function get(key: string, val?: { [key:string]: any }, onlyString?: false): string | { [key:string]: string };
function get(key: string, second?: boolean | { [key:string]: any }, third?: boolean) {
  // implements
}
@cwtuan
Copy link
Collaborator

cwtuan commented Nov 19, 2018

Will you use intl.get('categroy')?
I thought intl.get should always returns string.
In the case of intl.get('categroy'), should we make it return empty string?

@zry656565
Copy link
Author

zry656565 commented Nov 19, 2018

Will you use intl.get('categroy')?

I don't. But after upgrading, this change broke our buggy app, cause we wrote codes like intl.get('category').d('default val') and .d() isn't defined in a Object. I know it's our bug that we used a wrong key. but if you guys wanna include such a breaking change. Please consider bumping major version to v2.0, or mention it in change log.

I thought intl.get should always returns string.
In the case of intl.get('categroy'), should we make it return empty string?

Of course, that sounds better to me. thanks!

@cwtuan
Copy link
Collaborator

cwtuan commented Nov 19, 2018

Though it's a breaking change, we may not bump major version since few people would use it in that way. But we will mention it in release log. Is this ok for you guys?

@zry656565
Copy link
Author

So you are gonna to keep this change and update type definitions, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants