unsplash / intlc

Compile ICU messages into code. Supports TypeScript and JSX. No runtime.
MIT License
57 stars 3 forks source link

Don't output JS/TS static strings #52

Closed samhh closed 2 years ago

samhh commented 2 years ago

Some locales may legitimately not need a variable that others do, for example in the case of gender. When this happens, if there are no other interpolations, we're going to output incompatible types across locales:

export const en: (x: { gender: 'male' | 'female' }) => string = x => etc

export const es: string = etc

Functions and strings can't unionise (no such value), however functions/objects with omitted properties can. Specifically:

type Current
  = string
  | ((x: { gender: 'male' | 'female' }) => string)

// TypeScript will insist upon providing a full `x`
type Proposed
  = (() => string)
  | ((x: { gender: 'male' | 'female' }) => string)
OliverJAsh commented 2 years ago

functions/objects with omitted properties can

I had to test this so sharing my example for reference:

type English = (deps: { gender: string }) => string;
type Spanish = () => string;
type Translations = Spanish | English;
declare const t: Translations;
// No error ✅
t({ gender: "male" });
// Error ❌
t({});
samhh commented 2 years ago

^ And importantly it should error if you omit the argument.

OliverJAsh commented 2 years ago

Updated my example.