Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript error : can not access array elements #2189

Open
army-u8 opened this issue May 15, 2024 · 3 comments
Open

Typescript error : can not access array elements #2189

army-u8 opened this issue May 15, 2024 · 3 comments

Comments

@army-u8
Copy link

army-u8 commented May 15, 2024

馃挜 Regression Report

Appears type error when I use the json file as a resource ,but translations to a object using as const, it'll work.
image
image

Last working version

Worked up to version: 23.11.3

Stopped working in version: 23.11.3

To Reproduce

Steps to reproduce the behavior:
HTTP.json

{
  "403": "403 Forbidden \n You don鈥檛 have permission to access this page.",
  "500": "Looks like the server is taking too long to respond, this can be caused by either poor connectivity or an error with our servers. Please try again in a while.",
  "503": "Service temporarily unavailable.",
  "501": ["a", "b", "c", "d", "e"]
}
import HTTP from './HTTP.json';
const defaultNS = 'general';
const httpCode = {
  '403': '403 Forbidden \n You don鈥檛 have permission to access this page.',
  '500':
    'Looks like the server is taking too long to respond, this can be caused by either poor connectivity or an error with our servers. Please try again in a while.',
  '503': 'Service temporarily unavailable.',
  '501': ['a', 'b', 'c', 'd', 'e'],
} as const;
export const resources = {
  en: {
    general: {
      http: HTTP,
    },
  },
} as const;
i18next.init({
  lng: 'en',
  debug: true,
  defaultNS,
  resources,
});
i18n.d.ts
declare module "i18next" {
  interface CustomTypeOptions {
    defaultNS: typeof defaultNS;
    resources: (typeof resources)["en"];
  }
}

https://stackblitz.com/edit/solidjs-templates-zghu8f?file=src%2Fi18n.ts,src%2FApp.tsx,src%2Fi18n.d.ts,src%2FHTTP.json,tsconfig.json

Your Environment

  • runtime version: i.e. node v14, deno, browser xy
  • i18next version: i.e. 23.11.3
  • os: Mac,
@adrai
Copy link
Member

adrai commented May 15, 2024

I don't think this was working before v23.11.3... That may be since v22.x
That's probably because of the number... like it is with the interpolation: https://www.i18next.com/overview/typescript#not-working-interpolation-values

@marcalexiei marcalexiei removed their assignment May 15, 2024
@army-u8
Copy link
Author

army-u8 commented May 15, 2024

And also type issue that if i use snake_case in json the generate new field as snakease

{
    "403": "403 Forbidden \n You don鈥檛 have permission to access this page.",
    "500": "Looks like the server is taking too long to respond, this can be caused by either poor connectivity or an error with our servers. Please try again in a while.",
    "503": "Service temporarily unavailable.",
    "error_code": "error"
}
i18next.init({
  lng: "en",
  debug: true,
  ns,
  defaultNS,
  resources,
});
image

@adrai
Copy link
Member

adrai commented May 15, 2024

Like said... you need the as const or an interface: https://www.i18next.com/overview/typescript#not-working-interpolation-values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants