Skip to content
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

Enable strict mode in tsconfig and fix type errors #11200

Merged
merged 33 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
aca904f
Enable strict mode in tsconfig.json
jerelmiller Sep 7, 2023
0da3d3d
Fix type issues in utilities/graphql/transform
jerelmiller Sep 7, 2023
78ccf61
Use spread to push incoming objects in offsetLimitPagination
jerelmiller Sep 7, 2023
69d528a
Fix type issues in writeToStore test
jerelmiller Sep 7, 2023
e503cbb
Fix type issues in Concast
jerelmiller Sep 7, 2023
0fe49a0
Remove unused filterInPlace utility
jerelmiller Sep 7, 2023
e34d084
Add this type to itAsync
jerelmiller Sep 7, 2023
962bd47
Add ! annotation to mockLink
jerelmiller Sep 7, 2023
36129f8
Fix assignment from unknown in error link
jerelmiller Sep 7, 2023
cea054e
Fix type issue in readFromStore
jerelmiller Sep 7, 2023
bed3439
Fix type issues in useQuery
jerelmiller Sep 7, 2023
b568689
some progress
phryneas Sep 8, 2023
6498326
more type fixes
phryneas Sep 13, 2023
b8c6eb6
change `addExportedVariables` signature
phryneas Sep 13, 2023
c3a761d
better typing
phryneas Sep 13, 2023
9eb5e3e
undo deprecation
phryneas Sep 13, 2023
0966cc9
Merge branch 'main' into enable-strict-mode
phryneas Sep 13, 2023
a101977
test types
phryneas Sep 13, 2023
55a6351
add api extractor
phryneas Sep 14, 2023
f909921
add api reports
phryneas Sep 14, 2023
e1e2fbb
add workflow
phryneas Sep 14, 2023
0426c84
formatting
phryneas Sep 14, 2023
be151b0
package lock
phryneas Sep 14, 2023
c83b042
remove `ensureResult` for this PR, add `TODO` type
phryneas Sep 14, 2023
4aa485b
Merge branch 'pr/api-extractor' into enable-strict-mode
phryneas Sep 14, 2023
1e7c5c6
API updates for this PR
phryneas Sep 14, 2023
c080309
Merge remote-tracking branch 'origin/main' into enable-strict-mode
phryneas Nov 10, 2023
0b2cf25
size-limit
phryneas Nov 10, 2023
9303630
introduce IDE-only `tsconfig.json`
phryneas Nov 10, 2023
5e3fb70
formatting
phryneas Nov 10, 2023
04dcc0d
fix path
phryneas Nov 10, 2023
2cb5c83
Merge pull request #11359 from apollographql/pr/enable-test-ts
jerelmiller Nov 10, 2023
94b07f9
Add changeset
jerelmiller Nov 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/cache/inmemory/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ import { InMemoryCache } from "../inMemoryCache";
import { withErrorSpy, withWarningSpy } from "../../../testing";
import { TypedDocumentNode } from "../../../core";
import { extractFragmentContext } from "../helpers";
import { KeyFieldsFunction } from "../policies";
import { invariant } from "../../../utilities/globals";

const getIdField = ({ id }: { id: string }) => id;
const getIdField: KeyFieldsFunction = ({ id }) => {
invariant(typeof id === "string", "id is not a string");
return id;
};

describe("writing to the store", () => {
const cache = new InMemoryCache({
Expand Down Expand Up @@ -1293,19 +1298,17 @@ describe("writing to the store", () => {
}

testData.forEach((data) => {
data.mutation.definitions.forEach(
(definition: OperationDefinitionNode) => {
if (isOperationDefinition(definition)) {
definition.selectionSet.selections.forEach((selection) => {
if (isField(selection)) {
expect(
storeKeyNameFromField(selection, data.variables)
).toEqual(data.expected);
}
});
}
data.mutation.definitions.forEach((definition) => {
if (isOperationDefinition(definition)) {
definition.selectionSet.selections.forEach((selection) => {
if (isField(selection)) {
expect(storeKeyNameFromField(selection, data.variables)).toEqual(
data.expected
);
}
});
}
);
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ function firstMissing(tree: MissingTree): string | undefined {
return value;
});
} catch (result) {
return result;
return result as string;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/link/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function onError(errorHandler: ErrorHandler): ApolloLink {
},
});
} catch (e) {
errorHandler({ networkError: e, operation, forward });
errorHandler({ networkError: e as Error, operation, forward });
observer.error(e);
}

Expand Down
8 changes: 4 additions & 4 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ class InternalState<TData, TVariables extends OperationVariables> {
// useQuery method, so we can safely use these members in other/later methods
// without worrying they might be uninitialized.
private renderPromises: ApolloContextValue["renderPromises"];
private queryHookOptions: QueryHookOptions<TData, TVariables>;
private watchQueryOptions: WatchQueryOptions<TVariables, TData>;
private queryHookOptions!: QueryHookOptions<TData, TVariables>;
private watchQueryOptions!: WatchQueryOptions<TVariables, TData>;

private useOptions(options: QueryHookOptions<TData, TVariables>) {
const watchQueryOptions = this.createWatchQueryOptions(
Expand Down Expand Up @@ -461,8 +461,8 @@ class InternalState<TData, TVariables extends OperationVariables> {
private onCompleted(data: TData) {}
private onError(error: ApolloError) {}

private observable: ObservableQuery<TData, TVariables>;
private obsQueryFields: Omit<
private observable!: ObservableQuery<TData, TVariables>;
private obsQueryFields!: Omit<
ObservableQueryFields<TData, TVariables>,
"variables"
>;
Expand Down
4 changes: 2 additions & 2 deletions src/testing/core/itAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function wrap(key?: "only" | "skip" | "todo") {
) =>
(key ? it[key] : it)(
message,
function () {
function (this: unknown) {
return new Promise((resolve, reject) =>
callback.call(this, resolve, reject)
);
Expand All @@ -21,7 +21,7 @@ function wrap(key?: "only" | "skip" | "todo") {
const wrappedIt = wrap();

export const itAsync = Object.assign(
function (...args: Parameters<typeof wrappedIt>) {
function (this: unknown, ...args: Parameters<typeof wrappedIt>) {
return wrappedIt.apply(this, args);
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/testing/core/mocking/mockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function requestToKey(request: GraphQLRequest, addTypename: Boolean): string {
}

export class MockLink extends ApolloLink {
public operation: Operation;
public operation!: Operation;
public addTypename: Boolean = true;
public showWarnings: boolean = true;
private mockedResponsesByKey: { [key: string]: MockedResponse[] } = {};
Expand Down
14 changes: 0 additions & 14 deletions src/utilities/common/filterInPlace.ts

This file was deleted.

20 changes: 15 additions & 5 deletions src/utilities/graphql/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
FragmentSpreadNode,
VariableDefinitionNode,
ASTNode,
ASTVisitor,
ASTVisitFn,
InlineFragmentNode,
} from "graphql";
import { visit, Kind } from "graphql";
Expand All @@ -29,6 +29,12 @@ import type { FragmentMap } from "./fragments.js";
import { createFragmentMap } from "./fragments.js";
import { isArray, isNonEmptyArray } from "../common/arrays.js";

// https://github.com/graphql/graphql-js/blob/8d7c8fccf5a9846a50785de04abda58a7eb13fc0/src/language/visitor.ts#L20-L23
interface EnterLeaveVisitor<TVisitedNode extends ASTNode> {
readonly enter?: ASTVisitFn<TVisitedNode>;
readonly leave?: ASTVisitFn<TVisitedNode>;
}

export type RemoveNodeConfig<N> = {
name?: string;
test?: (node: N) => boolean;
Expand Down Expand Up @@ -208,8 +214,10 @@ export function removeDirectivesFromDocument(
// original doc immediately without any modifications.
let firstVisitMadeChanges = false;

const fieldOrInlineFragmentVisitor: ASTVisitor = {
enter(node: FieldNode | InlineFragmentNode) {
const fieldOrInlineFragmentVisitor: EnterLeaveVisitor<
FieldNode | InlineFragmentNode
> = {
enter(node) {
if (shouldRemoveField(node.directives)) {
firstVisitMadeChanges = true;
return null;
Expand Down Expand Up @@ -385,8 +393,10 @@ export function removeDirectivesFromDocument(
)
);

const enterVisitor: ASTVisitor = {
enter(node: FragmentSpreadNode | FragmentDefinitionNode) {
const enterVisitor: EnterLeaveVisitor<
FragmentSpreadNode | FragmentDefinitionNode
> = {
enter(node) {
if (fragmentWillBeRemoved(node.name.value)) {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/observables/Concast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ export class Concast<T> extends Observable<T> {
// Any Concast object can be trivially converted to a Promise, without
// having to create a new wrapper Observable. This promise provides an
// easy way to observe the final state of the Concast.
private resolve: (result?: T | PromiseLike<T>) => void;
private reject: (reason: any) => void;
public readonly promise = new Promise<T>((resolve, reject) => {
private resolve!: (result?: T | PromiseLike<T>) => void;
private reject!: (reason: any) => void;
public readonly promise = new Promise<T | undefined>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/policies/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function offsetLimitPagination<T = Reference>(
// to receive any arguments, so you might prefer to throw an
// exception here, instead of recovering by appending incoming
// onto the existing array.
merged.push.apply(merged, incoming);
merged.push(...incoming);
}
}

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"experimentalDecorators": true,
"outDir": "./dist",
"lib": ["es2015", "esnext.asynciterable", "dom"],
"jsx": "react"
"jsx": "react",
"strict": true
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["src/**/__tests__/**/*"]
Expand Down
Loading