-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expect.extend: not possible to correctly extend corresponding TypeScript types #10642
Comments
Yep, we need to figure out a good story for extending matchers for DefinitelyTyped/DefinitelyTyped#44365 (comment). I haven't spend any time on trying to come up with a good solution for it. We need to both support importing from |
@SimenB is changing the main export for This would make extending easily possible. It would also simplify this package. Merging the types and implementation (which are currently separate) might also be possible. |
Hi, I've encountered the same situation, it seems that the problem is a typescript problem with Declaration Merging. (see more here microsoft/TypeScript#26660 (comment)) I've create a What worked for medeclare namespace jest {
interface Matchers<R> {
toWorkProperly(a: number): R;
}
} and declare global {
namespace jest {
interface Matchers<R> {
toWorkProperly(a: number): R;
}
}
}
export {}; What doesn't workdeclare global {
namespace jest {
interface Matchers<R> {
toWorkProperly(a: number): R;
}
}
} and declare namespace jest {
interface Matchers<R> {
toWorkProperly(a: number): R;
}
}
export {}; I don't understand precisely the root cause, it seems to be linked to how ts handles modules and types files; |
Update doc to add types for custom matchers Fix : jestjs#10642
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
When extending expect
it should be possible to extend the corresponding types:
instead you get
To Reproduce
see above
Expected behavior
There should be no error and proper typechecking.
The reason for the issue, is that expect doesn't actually export the
Matchers
interface whichexpect()
returns, but rather a sub-interface. See https://github.com/SimenB/jest/blob/master/packages/expect/src/index.ts#L426 .Unfortunately, it isn't possible to do the right thing directly here, i.e., you can't re-export an interface in a namespace. The quick option would be to move the Matchers declaration directly into the namespace instead of importing it from './types'. The proper solution would probably be to use a proper ES export for expect.
Link to repl or repo (highly encouraged)
repl.it link clicky clicky
envinfo
The text was updated successfully, but these errors were encountered: