You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently discovered the @schummar/icu-type-parser package (npm, Github). The library allows to parse ICU messages with TypeScript types exclusively.
I think this is a great feature, and it would be great if formatjs had built-in support for this as well. It would be a nice complement to the existing runtime parser you already have @formatjs/icu-messageformat-parser
Here is an example of how the library works:
importtype{GetICUArgs}from'@schummar/icu-type-parser';importtype{PrimitiveType}from'intl-messageformat';constmessage=`hello {firstName},You have {nrMessages, number} new messages.Your birthDate is {birthDate, date}.Your friend called. {friendGender, select, male {He will call again later.} female {She will call again later.} other {They will call again later.}}`;typeIcuArgsOptions={ICUNumberArgument?: number;ICUDateArgument?: Date;ICUArgument?: PrimitiveType;};// ICU arguments and their types are inferredtypeIcuParameters=GetICUArgs<typeofmessage,IcuArgsOptions>;// "male" | "female" | "other" | (string & {})typeFriendGender=IcuParameters["friendGender"]// No TS errorconstparams1: IcuParameters={firstName: 'John',birthDate: newDate(),nrMessages: 10,friendGender: 'female',};// TS errorsconstparams2: IcuParameters={// <- misses properties `birthDate`, `friendGender`lastName: 'Smith',// <- `lastName` does not exist in type `IcuParameters`nrMessages: "10",// <- `string` is not assignable to `number`};
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I recently discovered the
@schummar/icu-type-parser
package (npm, Github). The library allows to parse ICU messages with TypeScript types exclusively.I think this is a great feature, and it would be great if
formatjs
had built-in support for this as well. It would be a nice complement to the existing runtime parser you already have@formatjs/icu-messageformat-parser
Here is an example of how the library works:
Beta Was this translation helpful? Give feedback.
All reactions