Romanchuk / angular-i18next

angular v10+ integration with i18next v19.4+
MIT License
131 stars 33 forks source link

Build error when updating to 14.2.0 with strict typing #97

Closed marcovmun closed 1 year ago

marcovmun commented 1 year ago

Build error when updating to 14.2.0

Compiling with Angular sources in Ivy partial compilation mode.
 ✖ Compiling with Angular sources in Ivy partial compilation mode.
node_modules/.pnpm/angular-i18next@14.2.0_ast4p4ycywo3bpfejuahudj7lm/node_modules/angular-i18next/lib/I18NextEvents.d.ts:5:5 - error TS2416: Property 'initialized' in type 'I18NextEvents' is not assignable to the same property in base type 'ITranslationEvents'.
   Type 'BehaviorSubject<InitOptions>' is not assignable to type 'BehaviorSubject<InitOptions | undefined>'.
     Types of property 'observers' are incompatible.
       Type 'Observer<InitOptions>[]' is not assignable to type 'Observer<InitOptions | undefined>[]'.
         Type 'Observer<InitOptions>' is not assignable to type 'Observer<InitOptions | undefined>'.
           Type 'InitOptions | undefined' is not assignable to type 'InitOptions'.
             Type 'undefined' is not assignable to type 'InitOptions'.

 5     initialized: BehaviorSubject<i18n.InitOptions>;
       ~~~~~~~~~~~
 node_modules/.pnpm/angular-i18next@14.2.0_ast4p4ycywo3bpfejuahudj7lm/node_modules/angular-i18next/lib/I18NextEvents.d.ts:11:5 - error TS2416: Property 'languageChanged' in type 'I18NextEvents' is not assignable to the same property in base type 'ITranslationEvents'.
   Type 'BehaviorSubject<string>' is not assignable to type 'BehaviorSubject<string | null>'.
     Types of property 'observers' are incompatible.
       Type 'Observer<string>[]' is not assignable to type 'Observer<string | null>[]'.
         Type 'Observer<string>' is not assignable to type 'Observer<string | null>'.
           Type 'string | null' is not assignable to type 'string'.
            Type 'null' is not assignable to type 'string'.

11     languageChanged: BehaviorSubject<string>;

I think has something to do with: tsconfig.json "compilerOptions": { "strict": true, },

SergejSintschilin commented 1 year ago

I have the same problem.

Romanchuk commented 1 year ago

Ok, thanks for report, i'll look for fix along with https://github.com/i18next/i18next/issues/1869

ellyxc commented 1 year ago

i have similar problem

hamodi commented 1 year ago

Hey, i am going through the same problem. Any idea when this would be resolved?

ellyxc commented 1 year ago

I endup changing my tsconfig file compilerOptions": { "strict": true, }, but i am still working for dev version, which is not acceptable for prod work

Romanchuk commented 1 year ago

@hamodi @ellyxc @SergejSintschilin @marcovmun Please try angular-i18next@15.0.0-0

luisgouveiaaa commented 1 year ago

@Romanchuk I'm also having this problem but I cannot upgrade to version 15.0.0-0 because its forcing angular version to be 15 and we're still at 14....

marcovmun commented 1 year ago

I still got compile errors:

node_modules/.pnpm/angular-i18next@15.0.0-0_vtlaauorlik3d7w2q37gaylcga/node_modules/angular-i18next/lib/ITranslationService.d.ts:16:105 - error TS2694: Namespace '"D:/projects/libs/ui-components/node_modules/.pnpm/i18next@21.10.0
/node_modules/i18next/index"' has no exported member 'DefaultTFuncReturn'.

16     t(key: string | string[], optionsOrDefault?: string | i18n.TOptions, options?: i18n.TOptions): i18n.DefaultTFuncReturn;
                                                                                                           ~~~~~~~~~~~~~~~~~~
node_modules/.pnpm/angular-i18next@15.0.0-0_vtlaauorlik3d7w2q37gaylcga/node_modules/angular-i18next/lib/I18NextService.d.ts:13:20 - error TS2315: Type 'InitOptions' is not generic.

13     get options(): i18n.InitOptions<object>;
                      ~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/.pnpm/angular-i18next@15.0.0-0_vtlaauorlik3d7w2q37gaylcga/node_modules/angular-i18next/lib/I18NextService.d.ts:22:105 - error TS2694: Namespace '"D:/projects/libs/ui-components/node_modules/.pnpm/i18next@21.10.0/node
_modules/i18next/index"' has no exported member 'DefaultTFuncReturn'.

22     t(key: string | string[], optionsOrDefault?: string | i18n.TOptions, options?: i18n.TOptions): i18n.DefaultTFuncReturn;
                                                                                                           ~~~~~~~~~~~~~~~~~~
node_modules/.pnpm/angular-i18next@15.0.0-0_vtlaauorlik3d7w2q37gaylcga/node_modules/angular-i18next/lib/I18NextEvents.d.ts:5:34 - error TS2315: Type 'InitOptions' is not generic.

5     initialized: BehaviorSubject<i18n.InitOptions<object>>;
                                   ~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/.pnpm/angular-i18next@15.0.0-0_vtlaauorlik3d7w2q37gaylcga/node_modules/angular-i18next/lib/I18NextEvents.d.ts:11:5 - error TS2416: Property 'languageChanged' in type 'I18NextEvents' is not assignable to the same prop
erty in base type 'ITranslationEvents'.
  Type 'BehaviorSubject<string>' is not assignable to type 'BehaviorSubject<string | null>'.
    Types of property 'observers' are incompatible.
      Type 'Observer<string>[]' is not assignable to type 'Observer<string | null>[]'.
        Type 'Observer<string>' is not assignable to type 'Observer<string | null>'.
          Type 'string | null' is not assignable to type 'string'.
            Type 'null' is not assignable to type 'string'.

11     languageChanged: BehaviorSubject<string>;
Romanchuk commented 1 year ago

@marcovmun Please share full contents of yours tsconfig.json I have "compilerOptions": { "strict": true, }, in this repo for demo app and this error never appears As i see you are using pnpm and looks like it doesn't get i18n from i18next/index

Romanchuk commented 1 year ago

@luisgouveiaaa Do you use pnpm too?

jemand771 commented 1 year ago

weirdly enough, I get the same error that's listed in the issue description with version 15.0.0-0

click to expand... ``` Error: node_modules/angular-i18next/lib/I18NextEvents.d.ts:5:5 - error TS2416: Property 'initialized' in type 'I18NextEvents' is not assignable to the same property in base type 'ITranslationEvents'. Type 'BehaviorSubject>' is not assignable to type 'BehaviorSubject | undefined>'. Types of property 'observers' are incompatible. Type 'Observer>[]' is not assignable to type 'Observer | undefined>[]'. Type 'Observer>' is not assignable to type 'Observer | undefined>'. Type 'InitOptions | undefined' is not assignable to type 'InitOptions'. Type 'undefined' is not assignable to type 'InitOptions'. 5 initialized: BehaviorSubject>; ~~~~~~~~~~~ Error: node_modules/angular-i18next/lib/I18NextEvents.d.ts:11:5 - error TS2416: Property 'languageChanged' in type 'I18NextEvents' is not assignable to the same property in base type 'ITranslationEvents'. Type 'BehaviorSubject' is not assignable to type 'BehaviorSubject'. Types of property 'observers' are incompatible. Type 'Observer[]' is not assignable to type 'Observer[]'. Type 'Observer' is not assignable to type 'Observer'. Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'. 11 languageChanged: BehaviorSubject; ~~~~~~~~~~~~~~~ ```

if I just do what the error says and edit ITranslationEvents.ts accordingly...

import { BehaviorSubject, Subject } from 'rxjs';
import * as i18n from 'i18next';

export type ResourceEvent = { lng: any; ns: any };
export type MissingKeyEvent = { lngs: any; namespace: any; key: any; res: any };

  export interface ITranslationEvents {
-   initialized: BehaviorSubject<i18n.InitOptions | undefined>;
+   initialized: BehaviorSubject<i18n.InitOptions>;
    loaded: BehaviorSubject<boolean>;
    failedLoading: Subject<any>;
    missingKey: Subject<MissingKeyEvent>;
    added: Subject<ResourceEvent>;
    removed: Subject<ResourceEvent>;
-   languageChanged: BehaviorSubject<string | null>;
+   languageChanged: BehaviorSubject<string>;
}

... I don't get any errors. I don't know why those are optional as I'm not familiar with any i18next internals whatsoever. I assume just removing the | undefined and | null has some unintended side effects

also, I had to upgrade to angular 15 to make the new version of your package work - maybe backport the fix to v14 once you/we figure out how to address this? 👀

luisgouveiaaa commented 1 year ago

@luisgouveiaaa Do you use pnpm too?

No, I'm using regular npm...

marcovmun commented 1 year ago
 "compilerOptions": {
        "alwaysStrict": true,
        "outDir": "dist/out-tsc",
        "forceConsistentCasingInFileNames": true,
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "noImplicitOverride": true,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "noImplicitThis": true,
        "importHelpers": true,
        "strictFunctionTypes": true,
        "strictBindCallApply": true,
        "target": "es2017",
        "module": "es2020",
        "lib": [
            "es2020",
            "dom"
        ]
    },

I have it really strict, but maybe I dit something wrong.

Romanchuk commented 1 year ago

@marcovmun Thanks, i will apply it to demo app tomorrow. It should help me to localize an issue

Romanchuk commented 1 year ago

@marcovmun What's your i18next version?

hamodi commented 1 year ago

sorry for editing but it is still failing to compile and only when setting strict to false does it work!

@Romanchuk @marcovmun @jemand771

Romanchuk commented 1 year ago

@marcovmun Please try to add: "exclude": ["node_modules"]

Romanchuk commented 1 year ago

I've figured out that ng-packagr removes null and undefined from field defenition... So you got an error. And this:

export class I18NextEvents implements ITranslationEvents {
  initialized = new BehaviorSubject<i18n.InitOptions | undefined>(undefined);
  loaded = new BehaviorSubject(false);
  failedLoading = new Subject();
  missingKey = new Subject<MissingKeyEvent>();
  added = new Subject<ResourceEvent>();
  removed = new Subject<ResourceEvent>();
  languageChanged = new BehaviorSubject<string | null>(null);
}

Becomes this:

export declare class I18NextEvents implements ITranslationEvents {
    initialized: BehaviorSubject<i18n.InitOptions<object>>;
    loaded: BehaviorSubject<boolean>;
    failedLoading: Subject<unknown>;
    missingKey: Subject<MissingKeyEvent>;
    added: Subject<ResourceEvent>;
    removed: Subject<ResourceEvent>;
    languageChanged: BehaviorSubject<string>;
}

Obviously if there is a solution - it is in library's tsconfig setup. Trying to find correct setup.. strictNullChecks doesn't help.

Anyway try to exclude 'node_modules' since there is no reason to check typings in 3rd party packages

Romanchuk commented 1 year ago

@marcovmun Completely changed build config (custom). Please try:
15.0.3

jemand771 commented 1 year ago

type errors are fixed for me from version 15.0.3 onwards, thanks! :D

although I had the same error as other people here, I'd like to wait for someone else to confirm 👀

Romanchuk commented 1 year ago

@luisgouveiaaa Do you still need fix for angular v14?

marcovmun commented 1 year ago

angular-i18next: 15.0.4 works with i18next 22 on my library, next week we are going to angular v15, so angular v14 is for us not important. Thank you for the fix and support. For me issue can be closed.

aamcatamney commented 1 year ago

I have found setting the "skipLibCheck": true in the tsconfig.json also helps

Romanchuk commented 1 year ago

Closing for now