Despite my experience using Typescript, I still come across challenges that stump me from time to time.
In a recent Github issue, the function passed into any test hook using a typed context is causing an error:
Property 'color' is missing in type '{ [key: string]: any; }' but required in type 'MyContext'
The code snippet from the Github issue triggering the problem is as follows:
interface MyContext {
color: string
}
interface TestControllerEx<TContext> extends Omit<TestController, 'ctx'> {
ctx: TContext;
}
fixture`Fixture1`.beforeEach(async (t: TestControllerEx<MyContext>) => {
t.ctx.color = 'red' // strongly typed!
});
test('Test1', async (t: TestControllerEx<MyContext>) => {
await t.expect(t.ctx.color).eql('red'); // 'red' strongly typed!
});
To work around the issue, I have made the color
property in MyContext
optional. However, I am keen to understand the root cause of this problem.
The TestController
interface in TestCafe types the ctx property as { [key: string]: any }
. Shouldn't the generic type passed into TestControllerEx
fulfill this requirement? I have attempted having TContext extend the { [key: string]: any }
index signature directly without success.
Is this issue related to TestCafe's typings or am I overlooking something specific to Typescript?
I would greatly appreciate any assistance. Thank you!