Skip to content

Commit 64305da

Browse files
authored
Merge pull request #1529 from undsoft/improve-typesafety
Improve type safety of TranslateService and TranslatePipe
2 parents e46a624 + d72850c commit 64305da

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

projects/ngx-translate/src/lib/translate.parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Parser', () => {
2929
});
3030

3131
it('should support interpolation functions', () => {
32-
const uc:InterpolateFunction = (params) => (params?.['x'] ?? '').toUpperCase() + ' YOU!';
32+
const uc:InterpolateFunction = (params) => ((params?.['x'] ?? '') as string).toUpperCase() + ' YOU!';
3333

3434
expect(parser.interpolate( uc , {"x":'bless'})).toBe('BLESS YOU!');
3535
});

projects/ngx-translate/src/lib/translate.pipe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class TranslatePipe implements PipeTransform, OnDestroy {
2727
constructor(private translate: TranslateService, private _ref: ChangeDetectorRef) {
2828
}
2929

30-
updateValue(key: string, interpolateParams?: object, translations?: InterpolatableTranslationObject): void {
30+
updateValue(key: string, interpolateParams?: InterpolationParameters, translations?: InterpolatableTranslationObject): void {
3131
const onTranslation = (res: Translation) => {
3232
this.value = res !== undefined ? res : key;
3333
this.lastKey = key;
@@ -45,7 +45,7 @@ export class TranslatePipe implements PipeTransform, OnDestroy {
4545
}
4646

4747
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
48-
transform(query: string, ...args: any[]): any {
48+
transform(query: string | undefined | null, ...args: any[]): Translation {
4949
if (!query || !query.length) {
5050
return query;
5151
}
@@ -55,7 +55,7 @@ export class TranslatePipe implements PipeTransform, OnDestroy {
5555
return this.value;
5656
}
5757

58-
let interpolateParams: object | undefined = undefined;
58+
let interpolateParams: InterpolationParameters | undefined = undefined;
5959
if (isDefinedAndNotNull(args[0]) && args.length) {
6060
if (isString(args[0]) && args[0].length) {
6161
// we accept objects written in the template such as {n:1}, {'n':1}, {n:'v'}

projects/ngx-translate/src/lib/translate.service.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ describe("TranslateService", () =>
998998
it("should handle language codes with underscores", () =>
999999
{
10001000
spyOnProperty(window.navigator, "language", "get").and.returnValue("en_US");
1001+
spyOnProperty(window.navigator, "languages", "get").and.returnValue(["en_US"]);
10011002

10021003
expect(translate.getBrowserLang()).toBe("en");
10031004
});

projects/ngx-translate/src/lib/translate.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export const USE_DEFAULT_LANG = new InjectionToken<string>('USE_DEFAULT_LANG');
1313
export const DEFAULT_LANGUAGE = new InjectionToken<string>('DEFAULT_LANGUAGE');
1414
export const USE_EXTEND = new InjectionToken<string>('USE_EXTEND');
1515

16-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17-
export type InterpolationParameters = Record<string, any>;
16+
export type InterpolationParameters = Record<string, unknown>;
1817

1918
export type Translation =
2019
string |

0 commit comments

Comments
 (0)