Skip to content

Can't implement Translator correctly with TSConfig strict #2528

@daniel-shuy

Description

@daniel-shuy

Describe the bug

When strictFunctionTypes and strictNullChecks are enabled in TSConfig (quite often as nowadays most TS projects have strict enabled), the Translator interface cannot be implemented correctly.

strictFunctionTypes requires function parameters to be contravariant and function return types to be covariant.

strictNullChecks causes string and string | undefined to be treated as different types.

Because the function implementation must match all 3 overloads:

export type Translator = {
(id: string, defaultMessage: string, values?: any): string;
(id: string, defaultMessage: undefined, values?: any): string | undefined;
(id: string, defaultMessage?: string, values?: any): string | undefined;
};

  • For the defaultMessage parameter, the only type that is contravariant with string, undefined and string | undefined is string | undefined
  • For the return type, the only type that is covariant with string and string | undefined is string

The only implementation that meets both is:

(id: string, defaultMessage?: string, values?: any) => string;

which means that the defaultMessage can be undefined, but the return value cannot be undefined.

This makes it impossible to implement the note in https://jsonforms.io/docs/i18n/#defaultmessage:

If the defaultMessage is undefined, you should also return undefined if there is no translation for the given key. Returning an empty string (or something similar) instead may result in undesired behavior. JSON Forms will use undefined when the message could be skipped or another more generic key could be tried.

Expected behavior

Able to implement the Translator interface as:

(id: string, defaultMessage?: string, values?: any) => string | undefined;

Steps to reproduce the issue

  1. Go to https://github.com/eclipsesource/jsonforms/blob/v3.7.0/packages/examples/tsconfig.json
  2. Set strictFunctionTypes and strictNullChecks to true in compilerOptions
  3. Build the project
  4. See that packages/examples/src/examples/i18n.ts fails to compile
    export const translate: Translator = (key: string, defaultMessage: string) => {
    return get(translations, key) ?? defaultMessage;
    };

Screenshots

No response

Which Version of JSON Forms are you using?

3.7.0

Package

Core

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions