问题
I tried to apply Aluan's answer in countless different ways, but none worked, that is, none gave me the sweet auto-complete for the types that I augmented:
augmentations.d.ts
import { Foo } from './foo';
declare module "mocha" {
namespace Mocha {
export interface Context {
foo: Foo;
}
}
}
I made sure that the augmentations.d.ts file is part of a glob pattern in the include property of tsconfig.json, but this is all I see in VSCode when I type this in a test hook:
Specifically mentioning this as the argument for the it hook didn't work either:
it("should do something", async function (this: Mocha.Context) {
...
});
回答1:
The solution was eventually provided by Aluan. There was a minor typo in his original answer.
Just remove the namespace:
import { Foo } from './foo';
declare module "mocha" {
export interface Context {
foo: Foo;
}
}
来源:https://stackoverflow.com/questions/62289629/how-to-augment-types-mocha