Skip to content

Commit e2fc385

Browse files
authored
Fix an invariance type error in the MockedResponse type. (#12920)
1 parent 52898f7 commit e2fc385

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

.api-reports/api-report-testing.api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export namespace MockLink {
4343
variables?: TVariables | VariableMatcher<TVariables>;
4444
}
4545
// (undocumented)
46-
export interface MockedResponse<out TData = Record<string, any>, out TVariables extends OperationVariables = Record<string, any>> {
46+
export interface MockedResponse<
47+
/** @ts-ignore */
48+
out TData = Record<string, any>, out TVariables extends OperationVariables = Record<string, any>> {
4749
// (undocumented)
4850
delay?: number | MockLink.DelayFunction;
4951
// (undocumented)

.changeset/chilled-pillows-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Fix an invariance type error in the `MockedResponse` type.

config/build.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { $ } from "zx";
55

66
import { babelTransform } from "./babel.ts";
77
import { compileTs } from "./compileTs.ts";
8-
import { reactCompiler } from "./react-compiler.ts";
98
import { deprecateInternals } from "./deprecateInternals.ts";
109
import { addExports } from "./exports.ts";
1110
import { distDir } from "./helpers.ts";
1211
import { inlineInheritDoc } from "./inlineInheritDoc.ts";
1312
import { prepareDist } from "./prepareDist.ts";
13+
import { preserveTsIgnore } from "./preserveTsIgnore.ts";
1414
import { processInvariants } from "./processInvariants.ts";
15+
import { reactCompiler } from "./react-compiler.ts";
1516
import { verifySourceMaps } from "./verifySourceMaps.ts";
1617
import { updateVersion, verifyVersion } from "./version.ts";
1718

@@ -42,6 +43,7 @@ const buildSteps = {
4243
updateVersion,
4344
inlineInheritDoc,
4445
deprecateInternals,
46+
preserveTsIgnore,
4547
processInvariants,
4648
reactCompiler,
4749
verifyVersion,

config/preserveTsIgnore.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* TypeScript removes normal comments when it creates declaration files.
3+
*
4+
* That means that `// @ts-ignore` comments are removed, which can lead to
5+
* unwanted errors in the generated declaration files, if those comments
6+
* are still necessary in the generated `.d.ts` files.
7+
*
8+
* As a workaround, it's possible to use `/** @ts-ignore ...` instead,
9+
* as these are preserved in the generated declaration files.
10+
* The downside to this is that these comments then also might end up in
11+
* IDE inline documentation, which we want to avoid.
12+
*
13+
* This build step post-processes all `.d.ts` files if a block-comment
14+
* starts with `* @ts-ignore`, it removes the leading `*`, effectively
15+
* turning them into non-docBlock comments for shipping.
16+
*/
17+
18+
import { visit } from "recast";
19+
20+
import type { BuildStep } from "./build.ts";
21+
import { applyRecast, frameComment } from "./helpers.ts";
22+
23+
export const preserveTsIgnore: BuildStep = async (options) =>
24+
applyRecast({
25+
glob: `**/*.{${options.jsExt},d.${options.tsExt}}`,
26+
cwd: options.targetDir,
27+
transformStep({ ast }) {
28+
return {
29+
ast: visit(ast, {
30+
visitNode(path) {
31+
this.traverse(path);
32+
const node = path.node;
33+
34+
if (!node.comments) {
35+
return;
36+
}
37+
38+
for (const comment of node.comments) {
39+
if (
40+
comment.type === "CommentBlock" &&
41+
comment.value.match(/^\*\s*@ts-ignore/)
42+
) {
43+
comment.value = comment.value.substring(1);
44+
}
45+
}
46+
},
47+
}),
48+
};
49+
},
50+
});

src/testing/core/mocking/mockLink.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export declare namespace MockLink {
5454
}
5555

5656
export interface MockedResponse<
57-
// @ts-ignore
57+
/** @ts-ignore */
5858
out TData = Record<string, any>,
5959
out TVariables extends OperationVariables = Record<string, any>,
6060
> {

0 commit comments

Comments
 (0)