Skip to content

Commit 0d761eb

Browse files
committed
fix: the type Translation not contains any again
Translation now extends StrictTranslation (without any that we use internally) with any. This makes the change more compatible with the previous release - and allows us to use the strict types internally.
1 parent c3c49dc commit 0d761eb

11 files changed

+139
-140
lines changed

projects/ngx-translate/src/lib/missing-translation-handler.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
MissingTranslationHandler,
55
MissingTranslationHandlerParams, provideTranslateService,
66
TranslateLoader,
7-
TranslateService, Translation,
8-
TranslationObject
7+
TranslateService, StrictTranslation,
8+
TranslationObject, Translation
99
} from "../public-api";
1010
import {Injectable, Type} from "@angular/core";
1111

@@ -37,7 +37,7 @@ describe('MissingTranslationHandler', () => {
3737

3838
@Injectable()
3939
class MissingObs implements MissingTranslationHandler {
40-
handle(params: MissingTranslationHandlerParams): Observable<Translation> {
40+
handle(params: MissingTranslationHandlerParams): Observable<StrictTranslation> {
4141
return of(`handled: ${params.key}`);
4242
}
4343
}
@@ -68,7 +68,7 @@ describe('MissingTranslationHandler', () => {
6868
translate.get('nonExistingKey').subscribe((res: Translation) => {
6969
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({key: 'nonExistingKey'}));
7070
//test that the instance of the last called argument is string
71-
expect(res as string).toEqual('handled');
71+
expect(res).toEqual('handled');
7272
});
7373
});
7474

@@ -81,7 +81,7 @@ describe('MissingTranslationHandler', () => {
8181
translate.get('nonExistingKey', interpolateParams).subscribe((res: Translation) => {
8282
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({interpolateParams: interpolateParams}));
8383
//test that the instance of the last called argument is string
84-
expect(res as string).toEqual('handled');
84+
expect(res).toEqual('handled');
8585
});
8686
});
8787

@@ -95,13 +95,13 @@ describe('MissingTranslationHandler', () => {
9595
.subscribe((res: Translation) => {
9696
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({translateService: translate}));
9797
//test that the instance of the last called argument is string
98-
expect(res as string).toEqual('handled');
98+
expect(res).toEqual('handled');
9999
});
100100
});
101101

102102
it('should return the key when using MissingTranslationHandler & the handler returns nothing', () => {
103103
class MissingUndef implements MissingTranslationHandler {
104-
handle(params: MissingTranslationHandlerParams):Translation|Observable<Translation> {
104+
handle(params: MissingTranslationHandlerParams):StrictTranslation|Observable<StrictTranslation> {
105105
void params;
106106
const data:TranslationObject = {};
107107
return data['test'];
@@ -114,7 +114,7 @@ describe('MissingTranslationHandler', () => {
114114

115115
translate.get('nonExistingKey').subscribe((res: Translation) => {
116116
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({key: 'nonExistingKey'}));
117-
expect(res as string).toEqual('nonExistingKey');
117+
expect(res).toEqual('nonExistingKey');
118118
});
119119
});
120120

@@ -133,7 +133,7 @@ describe('MissingTranslationHandler', () => {
133133
translate.use('en');
134134
spyOn(missingTranslationHandler, 'handle').and.callThrough();
135135

136-
expect(translate.instant('nonExistingKey') as string).toEqual('handled');
136+
expect(translate.instant('nonExistingKey')).toEqual('handled');
137137
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({key: 'nonExistingKey'}));
138138
});
139139

@@ -144,7 +144,7 @@ describe('MissingTranslationHandler', () => {
144144

145145
translate.get('nonExistingKey').subscribe((res: Translation) => {
146146
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({key: 'nonExistingKey'}));
147-
expect(res as string).toEqual('handled: nonExistingKey');
147+
expect(res).toEqual('handled: nonExistingKey');
148148
});
149149
});
150150

@@ -170,7 +170,7 @@ describe('MissingTranslationHandler', () => {
170170
translate.use('en');
171171
spyOn(missingTranslationHandler, 'handle').and.callThrough();
172172

173-
expect(translate.instant('nonExistingKey') as string).toEqual('nonExistingKey');
173+
expect(translate.instant('nonExistingKey')).toEqual('nonExistingKey');
174174
});
175175

176176
it('should not wait for the MissingTranslationHandler when it returns an observable & we use instant with an array', () => {
@@ -200,7 +200,7 @@ describe('MissingTranslationHandler', () => {
200200
translate.get('TEST').subscribe((res: Translation) => {
201201
expect(missingTranslationHandler.handle).toHaveBeenCalledWith(jasmine.objectContaining({key: 'TEST'}));
202202
//test that the instance of the last called argument is string
203-
expect(res as string).toEqual('handled');
203+
expect(res).toEqual('handled');
204204
});
205205
});
206206

@@ -211,7 +211,7 @@ describe('MissingTranslationHandler', () => {
211211

212212
spyOn(missingTranslationHandler, 'handle').and.callThrough();
213213
translate.get('TEST').subscribe((res: Translation) => {
214-
expect(res as string).toEqual('This is a test');
214+
expect(res).toEqual('This is a test');
215215
});
216216
});
217217
});

projects/ngx-translate/src/lib/missing-translation-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Injectable} from "@angular/core";
22
import {Observable} from "rxjs";
3-
import {TranslateService, Translation} from "./translate.service";
3+
import {TranslateService, StrictTranslation} from "./translate.service";
44

55
export interface MissingTranslationHandlerParams {
66
/**
@@ -30,7 +30,7 @@ export abstract class MissingTranslationHandler {
3030
* If it returns an observable, the value returned by this observable will be used (except if the method was "instant").
3131
* If it returns undefined, the key will be used as a value
3232
*/
33-
abstract handle(params: MissingTranslationHandlerParams): Translation|Observable<Translation>;
33+
abstract handle(params: MissingTranslationHandlerParams): StrictTranslation|Observable<StrictTranslation>;
3434
}
3535

3636
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ describe('TranslateCompiler', () => {
4848

4949
it('should use the compiler on loading translations', () => {
5050
translate.get('LOAD').subscribe((res: Translation) => {
51-
expect(res as string).toBe('This is a test');
51+
expect(res).toBe('This is a test');
5252
});
5353
});
5454

5555
it('should use the compiler on manually adding a translation object', () => {
5656
translate.setTranslation('en', {'SET-TRANSLATION': 'A manually added translation'});
57-
expect(translate.instant('SET-TRANSLATION') as string).toBe('A manually added translation');
57+
expect(translate.instant('SET-TRANSLATION')).toBe('A manually added translation');
5858
});
5959

6060
it('should use the compiler on manually adding a single translation', () => {
6161
translate.set('SET', 'Another manually added translation', 'en');
62-
expect(translate.instant('SET') as string).toBe('Another manually added translation');
62+
expect(translate.instant('SET')).toBe('Another manually added translation');
6363
});
6464
});
6565

@@ -101,18 +101,18 @@ describe('TranslateCompiler', () => {
101101

102102
it('should use the compiler on loading translations', () => {
103103
translate.get('LOAD').subscribe((res: Translation) => {
104-
expect(res as string).toBe('This is a test|compiled');
104+
expect(res).toBe('This is a test|compiled');
105105
});
106106
});
107107

108108
it('should use the compiler on manually adding a translation object', () => {
109109
translate.setTranslation('en', {'SET-TRANSLATION': 'A manually added translation'});
110-
expect(translate.instant('SET-TRANSLATION') as string).toBe('A manually added translation|compiled');
110+
expect(translate.instant('SET-TRANSLATION')).toBe('A manually added translation|compiled');
111111
});
112112

113113
it('should use the compiler on manually adding a single translation', () => {
114114
translate.set('SET', 'Another manually added translation', 'en');
115-
expect(translate.instant('SET') as string).toBe('Another manually added translation|compiled');
115+
expect(translate.instant('SET')).toBe('Another manually added translation|compiled');
116116
});
117117
});
118118
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
LangChangeEvent,
77
TranslateService,
88
TranslationChangeEvent,
9-
Translation,
9+
StrictTranslation,
1010
InterpolationParameters
1111
} from "./translate.service";
1212
import {equals, isDefinedAndNotNull, isString} from "./util";
@@ -127,7 +127,7 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
127127

128128
this.lastParams = this.currentParams;
129129

130-
const onTranslation = (res: Translation) => {
130+
const onTranslation = (res: StrictTranslation) => {
131131
if (res !== key || !node.lastKey) {
132132
node.lastKey = key;
133133
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('TranslateLoader', () => {
4141

4242
// this will request the translation from the backend because we use a static files loader for TranslateService
4343
translate.get('TEST').subscribe((res: Translation) => {
44-
expect(res as string).toEqual('This is a test');
44+
expect(res).toEqual('This is a test');
4545
});
4646
});
4747

@@ -70,7 +70,7 @@ describe('TranslateLoader', () => {
7070

7171
// this will request the translation from the CustomLoader
7272
translate.get('TEST').subscribe((res: Translation) => {
73-
expect(res as string).toEqual('This is also a test');
73+
expect(res).toEqual('This is also a test');
7474
});
7575
});
7676

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('TranslatePipe (modules)', () => {
9292
translate.setTranslation('en', {"TEST": "This is a test"});
9393
translate.use('en');
9494

95-
expect(translatePipe.transform('TEST') as string).toEqual("This is a test");
95+
expect(translatePipe.transform('TEST')).toEqual("This is a test");
9696
});
9797

9898
it('should call markForChanges when it translates a string', () => {
@@ -108,44 +108,44 @@ describe('TranslatePipe (modules)', () => {
108108
translate.setTranslation('en', {"TEST": "This is a test {{param}}"});
109109
translate.use('en');
110110

111-
expect(translatePipe.transform('TEST', {param: "with param"}) as string).toEqual("This is a test with param");
111+
expect(translatePipe.transform('TEST', {param: "with param"})).toEqual("This is a test with param");
112112
});
113113

114114
it('should translate a string with object as string parameters', () => {
115115
translate.setTranslation('en', {"TEST": "This is a test {{param}}"});
116116
translate.use('en');
117117

118-
expect(translatePipe.transform('TEST', '{param: "with param"}') as string).toEqual("This is a test with param");
119-
expect(translatePipe.transform('TEST', '{"param": "with param"}') as string).toEqual("This is a test with param");
120-
expect(translatePipe.transform('TEST', "{param: 'with param'}") as string).toEqual("This is a test with param");
121-
expect(translatePipe.transform('TEST', "{'param' : 'with param'}") as string).toEqual("This is a test with param");
118+
expect(translatePipe.transform('TEST', '{param: "with param"}')).toEqual("This is a test with param");
119+
expect(translatePipe.transform('TEST', '{"param": "with param"}')).toEqual("This is a test with param");
120+
expect(translatePipe.transform('TEST', "{param: 'with param'}")).toEqual("This is a test with param");
121+
expect(translatePipe.transform('TEST', "{'param' : 'with param'}")).toEqual("This is a test with param");
122122
});
123123

124124
it('should translate a string with object as multiple string parameters', () => {
125125
translate.setTranslation('en', {"TEST": "This is a test {{param1}} {{param2}}"});
126126
translate.use('en');
127127

128-
expect(translatePipe.transform('TEST', '{param1: "with param-1", param2: "and param-2"}') as string)
128+
expect(translatePipe.transform('TEST', '{param1: "with param-1", param2: "and param-2"}'))
129129
.toEqual("This is a test with param-1 and param-2");
130-
expect(translatePipe.transform('TEST', '{"param1": "with param-1", "param2": "and param-2"}') as string)
130+
expect(translatePipe.transform('TEST', '{"param1": "with param-1", "param2": "and param-2"}'))
131131
.toEqual("This is a test with param-1 and param-2");
132-
expect(translatePipe.transform('TEST', "{param1: 'with param-1', param2: 'and param-2'}") as string)
132+
expect(translatePipe.transform('TEST', "{param1: 'with param-1', param2: 'and param-2'}"))
133133
.toEqual("This is a test with param-1 and param-2");
134-
expect(translatePipe.transform('TEST', "{'param1' : 'with param-1', 'param2': 'and param-2'}") as string)
134+
expect(translatePipe.transform('TEST', "{'param1' : 'with param-1', 'param2': 'and param-2'}"))
135135
.toEqual("This is a test with param-1 and param-2");
136136
});
137137

138138
it('should translate a string with object as nested string parameters', () => {
139139
translate.setTranslation('en', {"TEST": "This is a test {{param.one}} {{param.two}}"});
140140
translate.use('en');
141141

142-
expect(translatePipe.transform('TEST', '{param: {one: "with param-1", two: "and param-2"}}') as string)
142+
expect(translatePipe.transform('TEST', '{param: {one: "with param-1", two: "and param-2"}}'))
143143
.toEqual("This is a test with param-1 and param-2");
144-
expect(translatePipe.transform('TEST', '{"param": {"one": "with param-1", "two": "and param-2"}}') as string)
144+
expect(translatePipe.transform('TEST', '{"param": {"one": "with param-1", "two": "and param-2"}}'))
145145
.toEqual("This is a test with param-1 and param-2");
146-
expect(translatePipe.transform('TEST', "{param: {one: 'with param-1', two: 'and param-2'}}") as string)
146+
expect(translatePipe.transform('TEST', "{param: {one: 'with param-1', two: 'and param-2'}}"))
147147
.toEqual("This is a test with param-1 and param-2");
148-
expect(translatePipe.transform('TEST', "{'param' : {'one': 'with param-1', 'two': 'and param-2'}}") as string)
148+
expect(translatePipe.transform('TEST', "{'param' : {'one': 'with param-1', 'two': 'and param-2'}}"))
149149
.toEqual("This is a test with param-1 and param-2");
150150
});
151151

@@ -156,15 +156,15 @@ describe('TranslatePipe (modules)', () => {
156156
spyOn(translatePipe, 'updateValue').and.callThrough();
157157
spyOn(ref, 'markForCheck').and.callThrough();
158158

159-
expect(translatePipe.transform('TEST', {param: "with param"}) as string).toEqual("This is a test with param");
159+
expect(translatePipe.transform('TEST', {param: "with param"})).toEqual("This is a test with param");
160160
expect(translatePipe.updateValue).toHaveBeenCalledTimes(1);
161161

162162
// same value, shouldn't call 'updateValue' again
163-
expect(translatePipe.transform('TEST', {param: "with param"}) as string).toEqual("This is a test with param");
163+
expect(translatePipe.transform('TEST', {param: "with param"})).toEqual("This is a test with param");
164164
expect(translatePipe.updateValue).toHaveBeenCalledTimes(1);
165165

166166
// different param, should call 'updateValue'
167-
expect(translatePipe.transform('TEST', {param: "with param2"}) as string).toEqual("This is a test with param2");
167+
expect(translatePipe.transform('TEST', {param: "with param2"})).toEqual("This is a test with param2");
168168
expect(translatePipe.updateValue).toHaveBeenCalledTimes(2);
169169

170170
expect(ref.markForCheck).toHaveBeenCalledTimes(2);
@@ -194,12 +194,12 @@ describe('TranslatePipe (modules)', () => {
194194
translate.setTranslation('fr', {"TEST": "C'est un test"});
195195
translate.use('en');
196196

197-
expect(translatePipe.transform('TEST') as string).toEqual("This is a test");
197+
expect(translatePipe.transform('TEST')).toEqual("This is a test");
198198

199199
// this will be resolved at the next lang change
200200
const subscription = translate.onLangChange.subscribe((res: LangChangeEvent) => {
201201
expect(res.lang).toEqual('fr');
202-
expect(translatePipe.transform('TEST') as string).toEqual("C'est un test");
202+
expect(translatePipe.transform('TEST')).toEqual("C'est un test");
203203
subscription.unsubscribe();
204204
done();
205205
});
@@ -209,14 +209,14 @@ describe('TranslatePipe (modules)', () => {
209209

210210
it('with file loader', (done) => {
211211
translate.use('en');
212-
expect(translatePipe.transform('TEST') as string).toEqual("This is a test");
212+
expect(translatePipe.transform('TEST')).toEqual("This is a test");
213213

214214
// this will be resolved at the next lang change
215215
const subscription = translate.onLangChange.subscribe((res: LangChangeEvent) => {
216216
// let it update the translations
217217
setTimeout(() => {
218218
expect(res.lang).toEqual('fr');
219-
expect(translatePipe.transform('TEST') as string).toEqual("C'est un test");
219+
expect(translatePipe.transform('TEST')).toEqual("C'est un test");
220220
subscription.unsubscribe();
221221
done();
222222
});
@@ -242,12 +242,12 @@ describe('TranslatePipe (modules)', () => {
242242
translate.setTranslation('fr', {"TEST": "C'est un test"});
243243
translate.setDefaultLang('en');
244244

245-
expect(translatePipe.transform('TEST') as string).toEqual("This is a test");
245+
expect(translatePipe.transform('TEST')).toEqual("This is a test");
246246

247247
// this will be resolved at the next lang change
248248
const subscription = translate.onDefaultLangChange.subscribe((res: DefaultLangChangeEvent) => {
249249
expect(res.lang).toEqual('fr');
250-
expect(translatePipe.transform('TEST') as string).toEqual("C'est un test");
250+
expect(translatePipe.transform('TEST')).toEqual("C'est un test");
251251
subscription.unsubscribe();
252252
done();
253253
});
@@ -257,14 +257,14 @@ describe('TranslatePipe (modules)', () => {
257257

258258
it('with file loader', (done) => {
259259
translate.setDefaultLang('en');
260-
expect(translatePipe.transform('TEST') as string).toEqual("This is a test");
260+
expect(translatePipe.transform('TEST')).toEqual("This is a test");
261261

262262
// this will be resolved at the next lang change
263263
const subscription = translate.onDefaultLangChange.subscribe((res: DefaultLangChangeEvent) => {
264264
// let it update the translations
265265
setTimeout(() => {
266266
expect(res.lang).toEqual('fr');
267-
expect(translatePipe.transform('TEST') as string).toEqual("C'est un test");
267+
expect(translatePipe.transform('TEST')).toEqual("C'est un test");
268268
subscription.unsubscribe();
269269
done();
270270
});

0 commit comments

Comments
 (0)