Skip to content

Commit 479f3ff

Browse files
Remove explicit any types from the codebase
This change removes all use of `any` from the code and updates the `no-explicit-any` eslint rule to be an error.
1 parent d9b886b commit 479f3ff

File tree

8 files changed

+41
-32
lines changed

8 files changed

+41
-32
lines changed

.changeset/cyan-crews-try.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Remove explicit `any` types from the codebase
6+
7+
This change removes all use of `any` from the code and updates the `no-explicit-any` eslint rule to be an error.

.vscode/settings.json

+4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
"editor.defaultFormatter": "esbenp.prettier-vscode",
33
"cSpell.words": [
44
"cfetch",
5+
"esbuild",
6+
"estree",
57
"execa",
68
"iarna",
79
"keyvalue",
10+
"middlewares",
811
"Miniflare",
12+
"outfile",
913
"Positionals"
1014
]
1115
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
],
7979
"no-shadow": "error",
8080
"@typescript-eslint/no-floating-promises": "error",
81+
"@typescript-eslint/no-explicit-any": "error",
8182
"no-empty": "off",
8283
"require-yield": "off",
8384
"no-empty-function": "off",

packages/wrangler/pages/functions/filepath-routing.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
31
import path from "path";
42
import fs from "fs/promises";
53
import { transform } from "esbuild";
64
import * as acorn from "acorn";
75
import * as acornWalk from "acorn-walk";
8-
import type { Config } from "./routes";
9-
import type { Identifier } from "estree";
10-
import type { ExportNamedDeclaration } from "@babel/types";
6+
import type { Config, RouteConfig } from "./routes";
7+
import type { ExportNamedDeclaration, Identifier } from "estree";
118

129
type Arguments = {
1310
baseDir: string;
@@ -18,10 +15,7 @@ export async function generateConfigFromFileTree({
1815
baseDir,
1916
baseURL,
2017
}: Arguments) {
21-
let routeEntries: [
22-
string,
23-
{ [key in "module" | "middleware"]?: string[] }
24-
][] = [] as any;
18+
let routeEntries: [string, RouteConfig][] = [];
2519

2620
if (!baseURL.startsWith("/")) {
2721
baseURL = `/${baseURL}`;
@@ -45,8 +39,10 @@ export async function generateConfigFromFileTree({
4539
sourceType: "module",
4640
});
4741
acornWalk.simple(ast, {
48-
ExportNamedDeclaration(_node) {
49-
const node: ExportNamedDeclaration = _node as any;
42+
ExportNamedDeclaration(_node: unknown) {
43+
// This dynamic cast assumes that the AST generated by acornWalk will generate nodes that
44+
// are compatible with the eslint AST nodes.
45+
const node = _node as ExportNamedDeclaration;
5046

5147
// this is an array because multiple things can be exported from a single statement
5248
// i.e. `export {foo, bar}` or `export const foo = "f", bar = "b"`

packages/wrangler/pages/functions/identifiers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const validIdentifierRegex = new RegExp(
6868
"u"
6969
);
7070

71-
export const isValidIdentifer = (identifier: string) =>
71+
export const isValidIdentifier = (identifier: string) =>
7272
validIdentifierRegex.test(identifier);
7373

7474
export const normalizeIdentifier = (identifier: string) =>

packages/wrangler/pages/functions/routes.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
31
import path from "path";
42
import fs from "fs/promises";
5-
import { isValidIdentifer, normalizeIdentifier } from "./identifiers";
3+
import { isValidIdentifier, normalizeIdentifier } from "./identifiers";
64

75
export const HTTP_METHODS = [
86
"HEAD",
@@ -17,7 +15,7 @@ export type HTTPMethod = typeof HTTP_METHODS[number];
1715
export function isHTTPMethod(
1816
maybeHTTPMethod: string
1917
): maybeHTTPMethod is HTTPMethod {
20-
return HTTP_METHODS.includes(maybeHTTPMethod as any);
18+
return (HTTP_METHODS as readonly string[]).includes(maybeHTTPMethod);
2119
}
2220

2321
export type RoutesCollection = Array<{
@@ -29,14 +27,16 @@ export type RoutesCollection = Array<{
2927

3028
export type Config = {
3129
routes?: RoutesConfig;
32-
schedules?: any;
30+
schedules?: unknown;
3331
};
3432

3533
export type RoutesConfig = {
36-
[route: string]: {
37-
middleware?: string | string[];
38-
module?: string | string[];
39-
};
34+
[route: string]: RouteConfig;
35+
};
36+
37+
export type RouteConfig = {
38+
middleware?: string | string[];
39+
module?: string | string[];
4040
};
4141

4242
type ImportMap = Map<
@@ -93,7 +93,7 @@ export function parseConfig(config: Config, baseDir: string) {
9393
}
9494

9595
// ensure the module name (if provided) is a valid identifier to guard against injection attacks
96-
if (name !== "default" && !isValidIdentifer(name)) {
96+
if (name !== "default" && !isValidIdentifier(name)) {
9797
throw new Error(`Invalid module identifier "${name}"`);
9898
}
9999

packages/wrangler/pages/functions/template-worker.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
31
import { match } from "path-to-regexp";
42
import type { HTTPMethod } from "./routes";
53

64
/* TODO: Grab these from @cloudflare/workers-types instead */
7-
type Params<P extends string = any> = Record<P, string | string[]>;
5+
type Params<P extends string = string> = Record<P, string | string[]>;
86

97
type EventContext<Env, P extends string, Data> = {
108
request: Request;
11-
waitUntil: (promise: Promise<any>) => void;
9+
waitUntil: (promise: Promise<unknown>) => void;
1210
next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
1311
env: Env & { ASSETS: { fetch: typeof fetch } };
1412
params: Params<P>;
@@ -17,7 +15,7 @@ type EventContext<Env, P extends string, Data> = {
1715

1816
declare type PagesFunction<
1917
Env = unknown,
20-
P extends string = any,
18+
P extends string = string,
2119
Data extends Record<string, unknown> = Record<string, unknown>
2220
> = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
2321
/* end @cloudflare/workers-types */
@@ -34,12 +32,12 @@ declare const routes: RouteHandler[];
3432

3533
// expect an ASSETS fetcher binding pointing to the asset-server stage
3634
type Env = {
37-
[name: string]: any;
35+
[name: string]: unknown;
3836
ASSETS: { fetch(url: string, init: RequestInit): Promise<Response> };
3937
};
4038

4139
type WorkerContext = {
42-
waitUntil: (promise: Promise<any>) => void;
40+
waitUntil: (promise: Promise<unknown>) => void;
4341
};
4442

4543
function* executeRequest(request: Request, env: Env) {
@@ -107,7 +105,11 @@ export default {
107105
const { value } = handlerIterator.next();
108106
if (value) {
109107
const { handler, params } = value;
110-
const context: EventContext<unknown, any, any> = {
108+
const context: EventContext<
109+
unknown,
110+
string,
111+
Record<string, unknown>
112+
> = {
111113
request: new Request(request.clone()),
112114
next,
113115
params,

packages/wrangler/src/cfetch/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,5 @@ function throwFetchError(
9898
}
9999

100100
function hasCursor(result_info: unknown): result_info is { cursor: string } {
101-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
102-
return (result_info as any)?.cursor !== undefined;
101+
return (result_info as { cursor } | undefined)?.cursor !== undefined;
103102
}

0 commit comments

Comments
 (0)