Skip to content

Commit 99ce1c1

Browse files
committed
feat(transloco): 🎸 improve strict mode types for pipe
Closes: #755
1 parent e6f1783 commit 99ce1c1

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

libs/transloco/src/lib/tests/pipe/pipe.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ describe('TranslocoPipe', () => {
2929
expect(pipe.transform('title', {})).toBe('');
3030
});
3131

32+
describe('None transformable values', () => {
33+
beforeEach(() => {
34+
pipe = new TranslocoPipe(serviceMock, undefined, 'es', cdrMock);
35+
});
36+
it('should handle null', () => {
37+
expect(pipe.transform(null)).toBeNull();
38+
});
39+
it('should handle undefined', () => {
40+
expect(pipe.transform(undefined)).toBeUndefined();
41+
});
42+
it('should handle empty string', () => {
43+
expect(pipe.transform('')).toBe('');
44+
});
45+
});
46+
3247
it('should use provided language', fakeAsync(() => {
3348
spyOn(serviceMock, 'translate').and.callThrough();
3449
pipe = new TranslocoPipe(serviceMock, undefined, 'es', cdrMock);

libs/transloco/src/lib/transloco.pipe.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,36 @@ export class TranslocoPipe implements PipeTransform, OnDestroy {
4646
this.scopeResolver = new ScopeResolver(this.service);
4747
}
4848

49-
// null is for handling strict mode + async pipe types https://github.com/jsverse/transloco/issues/311
50-
// null is for handling strict mode + optional chaining types https://github.com/jsverse/transloco/issues/488
49+
// overloads for strict mode
50+
transform(key: string, params?: HashMap, inlineLang?: string): string;
51+
transform(
52+
key: null | undefined,
53+
params?: HashMap,
54+
inlineLang?: string
55+
): null | undefined;
56+
transform(
57+
key: string | null,
58+
params?: HashMap,
59+
inlineLang?: string
60+
): string | null;
61+
transform(
62+
key: string | undefined,
63+
params?: HashMap,
64+
inlineLang?: string
65+
): string | undefined;
66+
transform(
67+
key: string | null | undefined,
68+
params?: HashMap,
69+
inlineLang?: string
70+
): string | null | undefined;
71+
5172
transform(
5273
key?: string | null,
5374
params?: HashMap,
5475
inlineLang?: string
55-
): string {
76+
): string | null | undefined {
5677
if (!key) {
57-
return key as any;
78+
return key;
5879
}
5980

6081
const keyName = params ? `${key}${JSON.stringify(params)}` : key;

0 commit comments

Comments
 (0)