Skip to content

Commit 95aa338

Browse files
Flow: Remove 'any's or improve their locality (#2433)
1 parent 791be1a commit 95aa338

File tree

6 files changed

+24
-32
lines changed

6 files changed

+24
-32
lines changed

src/error/__tests__/locatedError-test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ describe('locatedError', () => {
1919
});
2020

2121
it('passes GraphQLError-ish through', () => {
22-
const e: any = new Error('I have a different prototype chain');
23-
e.locations = [];
24-
e.path = [];
25-
e.nodes = [];
26-
e.source = null;
27-
e.positions = [];
28-
e.name = 'GraphQLError';
22+
const e = new Error('I have a different prototype chain');
23+
(e: any).locations = [];
24+
(e: any).path = [];
25+
(e: any).nodes = [];
26+
(e: any).source = null;
27+
(e: any).positions = [];
28+
(e: any).name = 'GraphQLError';
2929

3030
expect(locatedError(e, [], [])).to.deep.equal(e);
3131
});
3232

3333
it('does not pass through elasticsearch-like errors', () => {
34-
const e: any = new Error('I am from elasticsearch');
35-
e.path = '/something/feed/_search';
34+
const e = new Error('I am from elasticsearch');
35+
(e: any).path = '/something/feed/_search';
3636

3737
expect(locatedError(e, [], [])).to.not.deep.equal(e);
3838
});

src/execution/__tests__/executor-test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,8 @@ describe('Execute: Handles basic execution tasks', () => {
458458
return Promise.resolve(new Error('Error getting asyncReturnError'));
459459
},
460460
asyncReturnErrorWithExtensions() {
461-
const error: any = new Error(
462-
'Error getting asyncReturnErrorWithExtensions',
463-
);
464-
error.extensions = { foo: 'bar' };
461+
const error = new Error('Error getting asyncReturnErrorWithExtensions');
462+
(error: any).extensions = { foo: 'bar' };
465463

466464
return Promise.resolve(error);
467465
},

src/language/__tests__/visitor-test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ describe('Visitor', () => {
867867
});
868868

869869
describe('Support for custom AST nodes', () => {
870-
const customAST: any = parse('{ a }');
871-
customAST.definitions[0].selectionSet.selections.push({
870+
const customAST = parse('{ a }');
871+
(customAST: any).definitions[0].selectionSet.selections.push({
872872
kind: 'CustomField',
873873
name: {
874874
kind: 'Name',
@@ -916,10 +916,8 @@ describe('Visitor', () => {
916916
});
917917

918918
it('does not traverse unknown node kinds', () => {
919-
const customQueryDocumentKeys: any = {
920-
...QueryDocumentKeys,
921-
CustomField: ['name', 'selectionSet'],
922-
};
919+
const customQueryDocumentKeys = { ...QueryDocumentKeys };
920+
(customQueryDocumentKeys: any).CustomField = ['name', 'selectionSet'];
923921

924922
const visited = [];
925923
const visitor = {

src/polyfills/flatMap.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ declare function flatMap<T, U>(
55
fn: (item: T, index: number) => $ReadOnlyArray<U> | U,
66
): Array<U>;
77

8-
// Workaround to make older Flow versions happy
9-
const flatMapMethod = (Array.prototype: any).flatMap;
8+
const flatMapMethod = Array.prototype.flatMap;
109

1110
/* eslint-disable no-redeclare */
1211
// $FlowFixMe

src/utilities/extendSchema.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,7 @@ export function extendSchemaImpl(
404404
mutation: ?GraphQLObjectType,
405405
subscription: ?GraphQLObjectType,
406406
|} {
407-
// Note: While this could make early assertions to get the correctly
408-
// typed values below, that would throw immediately while type system
409-
// validation with validateSchema() will produce more actionable results.
410-
const opTypes: any = {};
407+
const opTypes = {};
411408
for (const node of nodes) {
412409
/* istanbul ignore next (See https://github.com/graphql/graphql-js/issues/2203) */
413410
const operationTypesNodes = node.operationTypes ?? [];
@@ -416,7 +413,11 @@ export function extendSchemaImpl(
416413
opTypes[operationType.operation] = getNamedType(operationType.type);
417414
}
418415
}
419-
return opTypes;
416+
417+
// Note: While this could make early assertions to get the correctly
418+
// typed values below, that would throw immediately while type system
419+
// validation with validateSchema() will produce more actionable results.
420+
return (opTypes: any);
420421
}
421422

422423
function getNamedType(node: NamedTypeNode): GraphQLNamedType {

src/validation/rules/UniqueDirectivesPerLocationRule.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { GraphQLError } from '../../error/GraphQLError';
44

55
import { Kind } from '../../language/kinds';
6-
import { type DirectiveNode } from '../../language/ast';
76
import { type ASTVisitor } from '../../language/visitor';
87

98
import { specifiedDirectives } from '../../type/directives';
@@ -44,12 +43,9 @@ export function UniqueDirectivesPerLocationRule(
4443
// them all, just listen for entering any node, and check to see if it
4544
// defines any directives.
4645
enter(node) {
47-
// Flow can't refine that node.directives will only contain directives,
48-
// so we cast so the rest of the code is well typed.
49-
const directives: ?$ReadOnlyArray<DirectiveNode> = (node: any).directives;
50-
if (directives) {
46+
if (node.directives != null) {
5147
const knownDirectives = Object.create(null);
52-
for (const directive of directives) {
48+
for (const directive of node.directives) {
5349
const directiveName = directive.name.value;
5450

5551
if (uniqueDirectiveMap[directiveName]) {

0 commit comments

Comments
 (0)