cibernox / svelte-intl-precompile

I18n library for Svelte.js that analyzes your keys at build time for max performance and minimal footprint
https://svelte-intl-precompile.com
ISC License
274 stars 13 forks source link

Fix import of `$locales` from Typescript project #57

Closed allezxandre closed 1 year ago

allezxandre commented 1 year ago

A top-level export in index.d.ts was preventing Typescript from using typings for $locales when imported from a Typescript project.

This PR fixes this by putting the export in a module declaration.

allezxandre commented 1 year ago

On a side note, Typescript will still complain about imports of actual locales in the form of import en from '$locales/en' as the $locales/* module is not defined.

cibernox commented 1 year ago

@allezxandre maybe there's a way of letting typescript know that $locales/<code> where code is any language code of the list of know language codes is valid.

In any case, this is a great improvement.

allezxandre commented 1 year ago

@allezxandre maybe there's a way of letting typescript know that $locales/<code> where code is any language code of the list of know language codes is valid.

Yes, setting something like so:

interface Locale {
    [key: string]: string;
}

declare module "$locales/*" {
    const locale: Locale;
    export default locale;
}

would work, but it would accept any language, including imports of random string like $locales/123R4ND0M.

There might be a better solution but alas I'm not experienced enough in Typescript module declarations to know 😕

cibernox commented 1 year ago

I don't see it very prioritary because most often than not apps should be using registerAll, unless they have a good reason not to (and since languages can be lazy loaded, there are very few good reasons)