茉莉花语境+打字稿 [英] Jasmine context + Typescript
问题描述
我将Jasmine与Typescript一起使用,最近我们开始在beforeEach
和it
中使用this
上下文.
I'm using Jasmine with Typescript and recently we started using the this
context in beforeEach
and it
.
示例:
beforeEach(() => {
this.fixture = TestBed.createComponent(blablabla);
});
it('should do something', () => {
expect(this.fixture).toBeTruthy();
});
问题在于TypeScript不够聪明,无法确定beforeEach
中的this
与it
中的this
完全相同.
有人知道这个事实的简单提示提示"吗?
The problem is that TypeScript is not smart enough to figure out that this
inside beforeEach
is exactly the same this
as in it
.
Does anyone know an easy way to 'hint' typescript about this fact?
这有可能吗?
推荐答案
您可以 typehint this
在函数中.实际上,我想,如果仅使用箭头功能(在describe
,beforeEach
和it
中),则上下文this
将是最外部的全局上下文.由于无法注释该注释,因此建议将常规的老式函数传递给最外面的describe
:
You can typehint this
in functions. Actually, if you only use arrow functions (in describe
, beforeEach
and it
), the context this
will be the outermost global context, I suppose. Since that one cannot be annotated, I suggest passing a regular old-style function to the outermost describe
:
// Dummy-type Jasmine functions (only for this MWE)
declare const describe: (msg: string, fun: () => void) => void;
declare const it: (msg: string, fun: () => void) => void;
declare const beforeEach: (fun: () => void) => void;
class A {
aProperty: string;
}
interface TestSuiteContext {
myObj: A;
}
describe('Test suite', function (this: TestSuiteContext) {
beforeEach(() => {
this.myObj = new A();
});
it('should do something', () => {
const message: string = this.myObj.aProperty;
});
});
这篇关于茉莉花语境+打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!