<any> 在 Typescript class 构造函数参数前面是什么意思?
What does <any> mean infront of an Typescript class constructor argument?
我正在阅读 Angular - Testing 文档。在描述如何测试异步 services(测试 HTTP 服务)时,我遇到了一个 class 构造函数,在传递的参数前面有一个 <any>
。完整的行由
给出
heroService = new HeroService(<any> httpClientSpy);
我知道,Typescript 中的 any
代表字面上的 "any" 类型。 guillemets (<...>
) 有什么用?为什么在参数前面打字?是否用于类型解析?
文档中的完整代码:
let httpClientSpy: { get: jasmine.Spy };
let heroService: HeroService;
beforeEach(() => {
// TODO: spy on other methods too
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
heroService = new HeroService(<any> httpClientSpy);
});
it('should return expected heroes (HttpClient called once)', () => {
const expectedHeroes: Hero[] =
[{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
heroService.getHeroes().subscribe(
heroes => expect(heroes).toEqual(expectedHeroes, 'expected heroes'),
fail
);
expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});
it('should return an error when the server returns a 404', () => {
const errorResponse = new HttpErrorResponse({
error: 'test 404 error',
status: 404, statusText: 'Not Found'
});
httpClientSpy.get.and.returnValue(asyncError(errorResponse));
heroService.getHeroes().subscribe(
heroes => fail('expected an error, not heroes'),
error => expect(error.message).toContain('test 404 error')
);
});
这是类型断言。
Originally the syntax that was added was <foo>
. However, there is an ambiguity in the language grammar when using <foo>
style assertions in JSX. Therefore it is now recommended that you just use as foo
for consistency.
https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html
HeroService 正在被实例化为 SpyObject
遵循结构 |Object
茉莉花可以打卡documentation
我正在阅读 Angular - Testing 文档。在描述如何测试异步 services(测试 HTTP 服务)时,我遇到了一个 class 构造函数,在传递的参数前面有一个 <any>
。完整的行由
heroService = new HeroService(<any> httpClientSpy);
我知道,Typescript 中的 any
代表字面上的 "any" 类型。 guillemets (<...>
) 有什么用?为什么在参数前面打字?是否用于类型解析?
文档中的完整代码:
let httpClientSpy: { get: jasmine.Spy };
let heroService: HeroService;
beforeEach(() => {
// TODO: spy on other methods too
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
heroService = new HeroService(<any> httpClientSpy);
});
it('should return expected heroes (HttpClient called once)', () => {
const expectedHeroes: Hero[] =
[{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
heroService.getHeroes().subscribe(
heroes => expect(heroes).toEqual(expectedHeroes, 'expected heroes'),
fail
);
expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});
it('should return an error when the server returns a 404', () => {
const errorResponse = new HttpErrorResponse({
error: 'test 404 error',
status: 404, statusText: 'Not Found'
});
httpClientSpy.get.and.returnValue(asyncError(errorResponse));
heroService.getHeroes().subscribe(
heroes => fail('expected an error, not heroes'),
error => expect(error.message).toContain('test 404 error')
);
});
这是类型断言。
Originally the syntax that was added was
<foo>
. However, there is an ambiguity in the language grammar when using<foo>
style assertions in JSX. Therefore it is now recommended that you just useas foo
for consistency.
https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html
HeroService 正在被实例化为 SpyObject
遵循结构 |Object
茉莉花可以打卡documentation