【发布时间】:2017-07-14 00:03:54
【问题描述】:
我正在使用 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)
})
【问题讨论】:
标签: typescript ava