openscd / open-scd-core

Apache License 2.0
5 stars 8 forks source link

Typing of PluginSet #128

Open danyill opened 1 year ago

danyill commented 1 year ago

I thought I could type some plugins like this:

const plugins: PluginSet = {
  menu: [
    {
      name: 'Subscriber Later Binding - Siemens',
      translations: {
        de: 'Kommunikationsexport',
        pt: 'Exportação de Comunicações',
      },
      icon: 'autorenew',
      active: true,
      requireDoc: true,
      src: '/dist/oscd-subscriber-lb-siemens.js',
    },
  ],
};

However I receive this error:

Type '{ de: string; pt: string; }' is not assignable to type 'Record<"de", string>'. Object literal may only specify known properties, and 'pt' does not exist in type 'Record<"de", string>'.ts(2322) open-scd.ts(37, 3): The expected type comes from property 'translations' which is declared here on type 'Plugin'

The type of translations comes from targetLocales

export type Plugin = {
  name: string;
  translations?: Record<typeof targetLocales[number], string>;
  src: string;
  icon: string;
  requireDoc?: boolean;
  active?: boolean;
};

This in turn is hard-coded to open-scd-core's targetLocales:

/**
 * The other locale codes that this application is localized into. Sorted
 * lexicographically.
 */
export const targetLocales = [
  `de`,
] as const;

I realise localisation isn't quite doing what we want at the moment (a la #121) but just filing this for reference. I might have guessed that core didn't have to support all the locales that plugins supported.