Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

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:

import type { GetICUArgs } from '@schummar/icu-type-parser';
import type { PrimitiveType } from 'intl-messageformat';

const message = `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.}
}`;

type IcuArgsOptions = {
  ICUNumberArgument?: number;
  ICUDateArgument?: Date;
  ICUArgument?: PrimitiveType;
};

// ICU arguments and their types are inferred
type IcuParameters = GetICUArgs<typeof message, IcuArgsOptions>;

// "male" | "female" | "other" | (string & {})
type FriendGender = IcuParameters["friendGender"]

// No TS error
const params1: IcuParameters = {
  firstName: 'John',
  birthDate: new Date(),
  nrMessages: 10,
  friendGender: 'female', 
};

// TS errors
const params2: IcuParameters = { // <- misses properties `birthDate`, `friendGender`
  lastName: 'Smith', // <- `lastName` does not exist in type `IcuParameters`
  nrMessages: "10", // <- `string` is not assignable to `number`
};
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.