我正在使用ava(无链接,因为我不允许使用超过2个)进行测试,并且想要输入ava的测试环境。这是typed as any
in ava's definition file。后续变量声明必须具有相同的类型。任何
我特别希望的是,打字稿编译器知道t.context
的类型是{foo: number}
在下面的测试:
import test from 'ava'
test.beforeEach((t) => {
t.context = { foo: 5 }
})
test('Is context typed', (t) => {
// uncaught typo
t.is(t.context.fooo, 5)
})
我试图用declaration merging要做到这一点,但它失败TS2403: Subsequent variable declarations must have the same type. Variable 'context' must be of type 'any', but here has type '{ foo: number; }'.
:
declare module 'ava' {
interface ContextualTestContext {
context: {
foo: number,
}
}
}
test.beforeEach((t) => {
t.context = { foo: 5 }
})
test('Is context typed', (t) => {
// uncaught ypo
t.is(t.context.fooo, 5)
})
有没有办法做到这一点没有铸造上下文像所有的时间,所以:
interface IMyContext {
foo: number
}
test.beforeEach((t) => {
t.context = { foo: 5 }
})
test('Is context typed', (t) => {
const context = <IMyContext> t.context
// caught typo
t.is(context.fooo, 5)
})
@despairblue你能为你的用例提出一个问题吗?也许我们可以在'test()'签名中接受一个通用的。 –
@MarkWubben谢谢,我在这里打开https://github.com/avajs/ava/issues/1291 – despairblue
@FinnO在技术上起作用,但如果我想使用'test'函数本身定义的函数(比如' test.beforeEach')我将不得不重新定义所有的声明。其中大约有1700个。 – despairblue