question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript payload not inferred in "new i18n"

See original GitHub issue

Unless I’m missing something, new i18n(config) does not infer the type of Params from config so @sveltekit-i18n/parser-default needs to be imported just to get the Parser type to declare it explicitly.

For translations.ts I am forced to do this so that my type checking doesn’t return an error:

import i18n from 'sveltekit-i18n';
import type { Config } from 'sveltekit-i18n';
import type { Parser } from '@sveltekit-i18n/parser-default';

export const defaultLocale = 'en';

export interface Payload extends Parser.PayloadDefault {
    placeholder?: string;
}

const config: Config<Payload> = {
   ...
};

export const { t, locale, locales, loading, loadTranslations, translations } = new i18n<
    Parser.Params<Payload>
>(config);

If I don’t include <Parser.Params<Payload>>, I get an error that placeholder doesn’t exist when passing it to a $t(...).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
jarda-svobodacommented, May 15, 2022

It works here, when @sveltekit-i18n/base and @sveltekit-i18n/parser-default are used separately, but it does not work under sveltekit-i18n, so you are 100% right about sveltekit-i18n declaration…

1reaction
ikorukcommented, May 15, 2022

I am not an expert in typescript but for example instead of:

declare class I18n<Params extends Parser.Params> extends Base<Params> {
    constructor(config?: Config);
    loadConfig: (config: Config) => Promise<void>;
}

This works for me:

declare class I18n<Params extends Parser.Params<P, M>, P = Parser.PayloadDefault, M = Modifier.DefaultProps> extends Base<Params> {
    constructor(config?: Config<P, M>);
    loadConfig: (config: Config<P, M>) => Promise<void>;
}

That way it just needs const config: Config<{param?: number}> and nothing else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am I getting "Type instantiation is excessively deep and ...
The AllModuleActions<M> type is recursive in a way that the compiler cannot handle very well. You are indexing arbitrarily deep into an ...
Read more >
Exact Types · Issue #12936 · microsoft/TypeScript - GitHub
Primary use-case is when you're creating a new type from ... Interface Inferred from Property Not Rejecting Invalid Properties #50732.
Read more >
Using with TypeScript - Ajv JSON schema validator
Note that it's currently not possible for JTDDataType to know whether the compiler is inferring timestamps as strings or Dates, and so it...
Read more >
Typescript and Redux. My tips. - DEV Community ‍ ‍
Each state in Redux should be immutable. Immutable object cannot be modified after it is created. If you forget this rule, your component...
Read more >
TypeScript with Options API - Vue.js
Typing Component Emits #. We can declare the expected payload type for an emitted event using the object syntax of the emits option....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found