i18next / i18next-browser-languageDetector

language detector used in browser environment for i18next
MIT License
865 stars 88 forks source link

Incorrect type definition for CustomDetector.lookup #245

Closed adriangodong closed 3 years ago

adriangodong commented 3 years ago

🐛 Bug Report

The type definition for CustomDetector.lookup is lookup(options: DetectorOptions): string | undefined;. In index.js and navigator.js, the return type can be a string array. Please update the type definition to lookup(options: DetectorOptions): string[] | string | undefined;

To Reproduce

Create a new custom detector and return an array (of string) in TypeScript, for example:

const MyDetector: CustomDetector = {
  name: "myDetector",
  lookup: (): string[] => []
}

Build error with Type 'string[]' is not assignable to type 'string'.ts(2322).

Expected behavior

No build error.

Your Environment

adrai commented 3 years ago

Can you try with v6.1.2 ?

adriangodong commented 3 years ago

Can you try with v6.1.2 ?

Yes, it works! Thank you for the quick fix.

wrobe0709 commented 3 years ago

Hi @adrai - I updated to 6.1.2 but I'm still getting an error related to the commit above. I'm running this in an Angular application and seeing the error while compiling.

i18next-browser-languagedetector version: 6.1.2 Typescript: 4.2.4

Error: node_modules/i18next-browser-languagedetector/index.d.ts:82:3 - error TS2416: Property 'detect' in type 'I18nextBrowserLanguageDetector' is not assignable to the same property in base type 'LanguageDetectorModule'.
  Type '(detectionOrder?: string[]) => string | string[]' is not assignable to type '() => string'.
    Type 'string | string[]' is not assignable to type 'string'.
      Type 'string[]' is not assignable to type 'string'.

     detect(detectionOrder?: DetectorOptions['order']): string | string[] | undefined;

Any help is appreciated, thanks!

adrai commented 3 years ago

@wrobe0709 please make sure you really updated all i18next dependencies... if the issue still persists, create a reproducible example.

wrobe0709 commented 3 years ago

@adrai - thanks for the quick response. I needed to update the i18Next package as well. Updating that to the latest got things working. Thanks again!