opral / inlang-paraglide-js

Tree-shakable i18n library build on the inlang ecosystem.
https://inlang.com/m/gerre34r/library-inlang-paraglideJs
42 stars 0 forks source link

Sveltekit `locals.paraglide.lang` isn't typed properly #23

Closed mquandalle closed 4 months ago

mquandalle commented 6 months ago

Problem

The locals.paraglide.lang should be typed with string literals corresponding to the supported languages instead of the generic string type, like createI18n keys for example.

Expected behavior

No response

Reproduction

const colors = {en: 'red', fr: 'blue', es: 'green'};
export function load ({locals}) {
    const color = colors[locals.paraglide.lang]; // type error: 'string' cannot index 'en' | 'fr' | 'es'
    return {color}
}

Other information

No response

LorisSigrist commented 6 months ago

Unfortunately I don't think it's possible to access the AvailableLanguageTag type from inside the Adapter package.

You can manually type it in src/app.d.ts:

import type { AvailableLanguageTag } from "$lib/paraglide/runtime.js"

declare global {
  namespace App {
    interface Locals {
      paraglide:  {
        lang: AvailableLanguageTag
        textDirection: "rtl" | "ltr"
      }
    }
  }
}
mquandalle commented 6 months ago

I suppose this file could be generated in the outdir? Sveltekit itself generate some typing files at dev/build type to type load function arguments.

(obviously using a relative import ./runtime.js instead of an import path that depends on the app config)

LorisSigrist commented 6 months ago

That could work, I'd have to prototype it

linear[bot] commented 6 months ago

PARJS-26 Sveltekit `locals.paraglide.lang` isn't typed properly