ttag icon indicating copy to clipboard operation
ttag copied to clipboard

Possible wrong type for LocaleData in index.d.ts

Open econdepe opened this issue 3 years ago • 1 comments

Hello,

I'm wondering if the type for Locale data in https://github.com/ttag-org/ttag/blob/master/types/index.d.ts#L16 shouldn't be export interface LocaleData { headers: Headers; translations?: Translations; contexts?: Translations; } Both the localeData validator or other functions accessing localeData like findTransObj expect the localeData object to contain either translations or contexts. Are those objects called localeData expected to have type LocaleData? If so, it looks like the exported type is not the proper one.

econdepe avatar Dec 16 '21 19:12 econdepe

I've encountered the same problem. So types should be more like this:

types/index.d.ts

// TypeScript Version: 2.3
export class StringWithRawData extends String {
    _strs: string[];
    _exprs: any[];
}

export interface Headers {
    'content-type'?: string;
    'plural-forms'?: string;
}

export interface Translations {
    [key: string]: any;
}

interface Contexts {
    [key: string]: Translations;
}

type LocaleData =
    | {
        headers: Headers;
        contexts: Contexts;
      }
    | {
        headers: Headers;
        translations: Translations;
      };

export function t(strings: TemplateStringsArray, ...expr: any[]): string;
export function jt(strings: TemplateStringsArray, ...expr: any[]): string | string[];
export function msgid(strings: TemplateStringsArray, ...expr: any[]): StringWithRawData;
export function gettext(id: string): string;
export function ngettext(...args: Array<(StringWithRawData|string|number)>): string;
export function addLocale(locale: string, data: LocaleData): void;
export function useLocale(locale: string): void;
export function setDedent(value: boolean): void;
export function setDefaultLang(lang: string): void;
export function useLocales(locales: string[]): void;

export interface BindedFunctions {
    t(strings: TemplateStringsArray, ...expr: any[]): string;
    jt(strings: TemplateStringsArray, ...expr: any[]): string | string[];
    gettext(id: string): string;
    ngettext(...args: Array<(StringWithRawData|string|number)>): string;
}

export function c(context: string): BindedFunctions;

May someone add a PR?

P.S. As a temporary solution you can add a ttag.d.ts in your project

import { Headers, Translations } from 'ttag/types';

declare module 'ttag' {
  interface Contexts {
    [context: string]: Translations;
  }

  interface LocaleData {
    headers: Headers;
    contexts?: Contexts;
    translations?: Translations;
  }
}

e-tsakhlo avatar Sep 06 '22 13:09 e-tsakhlo

Hi,

Is there any update on this issue? We have the same issue in one of our production projects.

hamidhre avatar Dec 05 '23 15:12 hamidhre

done in 1.8.6 :heavy_check_mark:

AlexMost avatar Jan 05 '24 12:01 AlexMost