Romanchuk / angular-i18next

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

Typing issue when `returnObjects: true` #121

Closed dingjules closed 3 months ago

dingjules commented 4 months ago

Reproduction: angular-i18next issue demo

While passing returnObjects: true to ITranslationService .t() I'm getting type match error Type 'string' is not assignable to type 'string[]'.

With returnObjects: true, the return type is supposed to be objects or arrays, but ITranslationService .t() will always return string.

I also checked directly with i18next.t() which works correctly by returning a $SpecialObject, the type is ready to be casted into custom types as described in doc

import * as i18n from 'i18next';
import { ITranslationService } from 'angular-i18next';

// i18next Correct returning types

// good
const _returnTypeIsString: string = i18n.t('some.string');

// good
const _returnTypeCanBeCasted: string[] = i18n.t('some.string', {
  returnObjects: true,
});

// angular-i18next always return string even with returnObjects: true,
const service1: ITranslationService = {
  t: () => '',
} as unknown as ITranslationService;

// good
const _returnTypeShouldBeString: string = service1.t('some.string');

// bad
const _returnTypeShouldBeCasted: string[] = service1.t('some.string', {
  returnObjects: true,
}); // Type 'string' is not assignable to type 'string[]'.(2322)
Romanchuk commented 3 months ago

Thanks, will check it out on this week

Romanchuk commented 3 months ago

Version: v17.0.2 released