From 2d7f57a5f99772c5d33eb1d20d154f4fca5746fd Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Tue, 2 Apr 2024 15:56:14 -0700 Subject: [PATCH] fix: enable strict mode for TS types --- lib/reactotron-core-client/package.json | 2 +- .../src/plugins/benchmark.ts | 2 +- .../src/reactotron-core-client.ts | 43 ++++++++++--------- lib/reactotron-core-client/src/serialize.ts | 8 ++-- lib/reactotron-core-client/src/validate.ts | 9 ++-- lib/reactotron-core-client/tsconfig.json | 1 + yarn.lock | 2 +- 7 files changed, 36 insertions(+), 31 deletions(-) diff --git a/lib/reactotron-core-client/package.json b/lib/reactotron-core-client/package.json index 606153113..f6fddcd8e 100644 --- a/lib/reactotron-core-client/package.json +++ b/lib/reactotron-core-client/package.json @@ -74,7 +74,7 @@ "rollup-plugin-resolve": "0.0.1-predev.1", "ts-jest": "^29.1.1", "tslib": "^2.6.2", - "typescript": "^4.9.5", + "typescript": "^5.1.3", "ws": "^8.14.2" }, "eslintConfig": { diff --git a/lib/reactotron-core-client/src/plugins/benchmark.ts b/lib/reactotron-core-client/src/plugins/benchmark.ts index 37e71f7aa..4d53bd9ba 100644 --- a/lib/reactotron-core-client/src/plugins/benchmark.ts +++ b/lib/reactotron-core-client/src/plugins/benchmark.ts @@ -7,7 +7,7 @@ const benchmark = () => (reactotron: ReactotronCore) => { const { startTimer } = reactotron const benchmark = (title: string) => { - const steps = [] + const steps = [] as Array<{title: string, time: number, delta: number}> const elapsed = startTimer() const step = (stepTitle: string) => { const previousTime = steps.length === 0 ? 0 : (steps[steps.length - 1] as any).time diff --git a/lib/reactotron-core-client/src/reactotron-core-client.ts b/lib/reactotron-core-client/src/reactotron-core-client.ts index 755b9ced8..792e197d3 100644 --- a/lib/reactotron-core-client/src/reactotron-core-client.ts +++ b/lib/reactotron-core-client/src/reactotron-core-client.ts @@ -101,7 +101,7 @@ export interface ReactotronCore { important?: boolean ) => void display: (config: DisplayConfig) => void - onCustomCommand: ( + onCustomCommand: >( config: CustomCommand ) => () => void | ((config: string, optHandler?: () => void) => () => void) /** @@ -161,9 +161,11 @@ function emptyPromise() { return Promise.resolve("") } -export class ReactotronImpl implements ReactotronCore { +export class ReactotronImpl + implements Omit +{ // the configuration options - options: ClientOptions + options!: ClientOptions /** * Are we connected to a server? @@ -173,7 +175,7 @@ export class ReactotronImpl implements ReactotronCore { /** * The socket we're using. */ - socket: WebSocket = null + socket: WebSocket = null as never /** * Available plugins. @@ -221,7 +223,7 @@ export class ReactotronImpl implements ReactotronCore { // options get merged & validated before getting set const newOptions = Object.assign( { - createSocket: null, + createSocket: null as never, host: "localhost", port: 9090, name: "reactotron-core-client", @@ -240,10 +242,11 @@ export class ReactotronImpl implements ReactotronCore { // if we have plugins, let's add them here if (Array.isArray(this.options.plugins)) { - this.options.plugins.forEach((p) => this.use(p)) + this.options.plugins.forEach((p) => this.use(p as never)) } - return this as this & InferFeaturesFromPlugins["plugins"]> + return this as this & + InferFeaturesFromPlugins["plugins"], undefined>> } close() { @@ -270,7 +273,7 @@ export class ReactotronImpl implements ReactotronCore { // establish a connection to the server const protocol = secure ? "wss" : "ws" - const socket = createSocket(`${protocol}://${host}:${port}`) + const socket = createSocket!(`${protocol}://${host}:${port}`) // fires when we talk to the server const onOpen = () => { @@ -282,7 +285,7 @@ export class ReactotronImpl implements ReactotronCore { const getClientIdPromise = getClientId || emptyPromise - getClientIdPromise(name).then((clientId) => { + getClientIdPromise(name!).then((clientId) => { this.isReady = true // introduce ourselves this.send("client.intro", { @@ -352,7 +355,7 @@ export class ReactotronImpl implements ReactotronCore { } // this is ws style from require('ws') on node js - if ("on" in socket && socket.on) { + if ("on" in socket && socket.on!) { const nodeWebSocket = socket as WebSocket nodeWebSocket.on("open", onOpen) nodeWebSocket.on("close", onClose) @@ -461,7 +464,7 @@ export class ReactotronImpl implements ReactotronCore { // here's how we're going to inject these in const inject = (key: string) => { // grab the function - const featureFunction = plugin.features[key] + const featureFunction = plugin.features![key] // only functions may pass if (typeof featureFunction !== "function") { @@ -494,20 +497,20 @@ export class ReactotronImpl implements ReactotronCore { onCustomCommand(config: CustomCommand | string, optHandler?: () => void): () => void { let command: string let handler: () => void - let title: string - let description: string - let args: CustomCommandArg[] + let title!: string + let description!: string + let args!: CustomCommandArg[] if (typeof config === "string") { command = config - handler = optHandler + handler = optHandler! } else { command = config.command handler = config.handler - title = config.title - description = config.description - args = config.args + title = config.title! + description = config.description! + args = config.args! } // Validations @@ -535,7 +538,7 @@ export class ReactotronImpl implements ReactotronCore { } if (args) { - const argNames = [] + const argNames = [] as string[] args.forEach((arg) => { if (!arg.name) { @@ -592,5 +595,5 @@ export function createClient( options?: ClientOptions ) { const client = new ReactotronImpl() - return client.configure(options as unknown) as unknown as Client + return client.configure(options as never) as unknown as Client } diff --git a/lib/reactotron-core-client/src/serialize.ts b/lib/reactotron-core-client/src/serialize.ts index 9c69c6359..a42280b9f 100644 --- a/lib/reactotron-core-client/src/serialize.ts +++ b/lib/reactotron-core-client/src/serialize.ts @@ -49,9 +49,9 @@ function getFunctionName(fn: any): string { * * @param {any} source - The victim. */ -function serialize(source, proxyHack = false) { - const stack = [] - const keys = [] +function serialize(source: any, proxyHack = false) { + const stack = [] as any[] + const keys = [] as string[] /** * Replace this object node with something potentially custom. @@ -60,7 +60,7 @@ function serialize(source, proxyHack = false) { * @param {*} value - The value to replace. */ function serializer(replacer) { - return function (this: any, key, value) { + return function (this: any, key: string, value: any) { // slam dunks if (value === true) return true diff --git a/lib/reactotron-core-client/src/validate.ts b/lib/reactotron-core-client/src/validate.ts index c70bc6b15..991542e6e 100644 --- a/lib/reactotron-core-client/src/validate.ts +++ b/lib/reactotron-core-client/src/validate.ts @@ -5,7 +5,8 @@ const isCreateSocketValid = ( createSocket: unknown ): createSocket is ClientOptions["createSocket"] => typeof createSocket !== "undefined" && createSocket !== null -const isHostValid = (host: string): boolean => typeof host === "string" && host && host !== "" +const isHostValid = (host: string): boolean => + (typeof host === "string" && host && host !== "") as boolean const isPortValid = (port: number): boolean => typeof port === "number" && port >= 1 && port <= 65535 const onCommandValid = (fn: (cmd: string) => any) => typeof fn === "function" @@ -21,15 +22,15 @@ const validate = (options: ClientOptions) => { throw new Error("invalid createSocket function") } - if (!isHostValid(host)) { + if (!isHostValid(host!)) { throw new Error("invalid host") } - if (!isPortValid(port)) { + if (!isPortValid(port!)) { throw new Error("invalid port") } - if (!onCommandValid(onCommand)) { + if (!onCommandValid(onCommand!)) { throw new Error("invalid onCommand handler") } } diff --git a/lib/reactotron-core-client/tsconfig.json b/lib/reactotron-core-client/tsconfig.json index 83f6d142e..46a102175 100644 --- a/lib/reactotron-core-client/tsconfig.json +++ b/lib/reactotron-core-client/tsconfig.json @@ -4,6 +4,7 @@ "allowJs": false, "declaration": true, "rootDir": ".", + "strict": true, "declarationDir": "dist/types", "emitDeclarationOnly": true, "emitDecoratorMetadata": true, diff --git a/yarn.lock b/yarn.lock index ae0d1d7e6..888472fa7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -24568,7 +24568,7 @@ __metadata: rollup-plugin-resolve: "npm:0.0.1-predev.1" ts-jest: "npm:^29.1.1" tslib: "npm:^2.6.2" - typescript: "npm:^4.9.5" + typescript: "npm:^5.1.3" ws: "npm:^8.14.2" languageName: unknown linkType: soft