angular ngx-translate error in TypeScript 4.5.5

Viewed 134

i'm following this tutoria; ([https://www.carloscaballero.io/angular-ngx-translate-typings/][1]) to using ngx-translate.

when i made a service for translate(translation.service.ts) i got an error:

(parameter) objectToTransform: string | object Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'string | object'. No index signature with a parameter of type 'string' was found on type 'string | object'.

in the below code line 31 ...objectToTransform[key] = transformObjectToPath(...

import { Injectable } from '@angular/core';
import eng from '../../assets/i18n/eng';


@Injectable({
  providedIn: 'root'
})
export class TranslationsService extends GenericClass<typeof eng>() {
  constructor() {
    super();
    Object.assign(this, transformObjectToPath('', eng));
  }
}

export function GenericClass<Props>(): new () => Props {
  return class { } as any;
}

function concatIfExistsPath(path: string, suffix: string): string {
  return path ? `${path}.${suffix}` : suffix;
}

function transformObjectToPath<T extends object | string>(
  suffix: string,
  objectToTransformOrEndOfPath: T,
  path = ''
): T {
  return typeof objectToTransformOrEndOfPath === 'object'
    ? Object.entries(objectToTransformOrEndOfPath).reduce(
      (objectToTransform, [key, value]) => {
        objectToTransform[key] = transformObjectToPath(
          key,
          value,
          concatIfExistsPath(path, suffix)
        );

        return objectToTransform;
      },
      {} as T
    )
    : (concatIfExistsPath(path, suffix) as T);
}

i think the code is in TypeScript 2.5 and i use TypeScript 4.5

Please help me fix the problem. regards

0 Answers
Related