messageformat / messageformat

ICU MessageFormat for Javascript - i18n Plural and Gender Capable Messages
https://messageformat.github.io/messageformat/
MIT License
1.69k stars 155 forks source link

Extend MessageFunction param typings #350

Closed dpchamps closed 2 years ago

dpchamps commented 2 years ago

MessageFunction is typed like so:

export type MessageFunction<ReturnType extends 'string' | 'values'> = (
  param?: Record<string, unknown>
) => ReturnType extends 'string' ? string : unknown[];

However, icu supports index lookup. For example, I could specify something like

On {1,date,::dMMMM}, there was {2} on planet {0,number,integer}.

And this will produce valid, expected results:

  const mf = new MessageFormat("*");
  const result = mf.compile([42, new Date("5/25/1977"), "a disturbance in the force"]);

  console.log(result) // On May 25, there was a disturbance in the force on planet 42.  

The type for the parameter should be Record<string, unknown> | unknown[]

eemeli commented 2 years ago

Fixed by #351