Skip to content

Commit fd8cf47

Browse files
authored
fix: enable strict mode for TS types (#1466)
## Please verify the following: - [x] `yarn build-and-test:local` passes - [x] I have added tests for any new features, if relevant - [x] `README.md` (or relevant documentation) has been updated with your changes ## Describe your PR This PR enables strict mode for the `reactotron-core-client` codebase to fix issues documented here: #1430
1 parent 4ed6195 commit fd8cf47

File tree

11 files changed

+46
-41
lines changed

11 files changed

+46
-41
lines changed

lib/reactotron-core-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"rollup-plugin-resolve": "0.0.1-predev.1",
7575
"ts-jest": "^29.1.1",
7676
"tslib": "^2.6.2",
77-
"typescript": "^4.9.5",
77+
"typescript": "^5.1.3",
7878
"ws": "^8.14.2"
7979
},
8080
"eslintConfig": {

lib/reactotron-core-client/src/client-options.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ type BrowserWebSocket = WebSocket
66
/**
77
* Configuration options for the Reactotron Client.
88
*/
9-
export interface ClientOptions<Client> extends LifeCycleMethods {
9+
export interface ClientOptions<Client> extends Omit<LifeCycleMethods, "onCommand"> {
1010
/**
1111
* A function which returns a websocket.
1212
*
1313
* This is over-engineered because we need the ability to create different
1414
* types of websockets for React Native, React, and NodeJS. :|
1515
*/
16-
createSocket?: ((path: string) => BrowserWebSocket) | ((path: string) => NodeWebSocket)
16+
createSocket?: ((path: string) => BrowserWebSocket) | ((path: string) => NodeWebSocket) | null
1717

1818
/**
1919
* The hostname or ip address of the server. Default: localhost.
2020
*/
21-
host?: string
21+
host?: string | null
2222

2323
/**
2424
* The port to connect to the server on. Default: 9090.
2525
*/
26-
port?: number
26+
port?: number | null
2727

2828
/**
2929
* The name of this client. Usually the app name.
@@ -55,7 +55,7 @@ export interface ClientOptions<Client> extends LifeCycleMethods {
5555
/**
5656
* Fires when the server sends a command.
5757
*/
58-
onCommand?: (command: any) => void
58+
onCommand?: ((command: any) => void) | null
5959

6060
/**
6161
* Fires when we connect to the server.

lib/reactotron-core-client/src/plugins/benchmark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const benchmark = () => (reactotron: ReactotronCore) => {
77
const { startTimer } = reactotron
88

99
const benchmark = (title: string) => {
10-
const steps = []
10+
const steps = [] as Array<{title: string, time: number, delta: number}>
1111
const elapsed = startTimer()
1212
const step = (stepTitle: string) => {
1313
const previousTime = steps.length === 0 ? 0 : (steps[steps.length - 1] as any).time

lib/reactotron-core-client/src/reactotron-core-client.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export interface ReactotronCore {
101101
important?: boolean
102102
) => void
103103
display: (config: DisplayConfig) => void
104-
onCustomCommand: <Args extends CustomCommandArg[] = CustomCommand["args"]>(
104+
onCustomCommand: <Args extends CustomCommandArg[] = Exclude<CustomCommand["args"], undefined>>(
105105
config: CustomCommand<Args>
106106
) => () => void | ((config: string, optHandler?: () => void) => () => void)
107107
/**
@@ -161,9 +161,11 @@ function emptyPromise() {
161161
return Promise.resolve("")
162162
}
163163

164-
export class ReactotronImpl implements ReactotronCore {
164+
export class ReactotronImpl
165+
implements Omit<ReactotronCore, "options" | "plugins" | "configure" | "connect" | "use">
166+
{
165167
// the configuration options
166-
options: ClientOptions<ReactotronCore>
168+
options!: ClientOptions<ReactotronCore>
167169

168170
/**
169171
* Are we connected to a server?
@@ -173,7 +175,7 @@ export class ReactotronImpl implements ReactotronCore {
173175
/**
174176
* The socket we're using.
175177
*/
176-
socket: WebSocket = null
178+
socket: WebSocket = null as never
177179

178180
/**
179181
* Available plugins.
@@ -221,7 +223,7 @@ export class ReactotronImpl implements ReactotronCore {
221223
// options get merged & validated before getting set
222224
const newOptions = Object.assign(
223225
{
224-
createSocket: null,
226+
createSocket: null as never,
225227
host: "localhost",
226228
port: 9090,
227229
name: "reactotron-core-client",
@@ -240,10 +242,11 @@ export class ReactotronImpl implements ReactotronCore {
240242

241243
// if we have plugins, let's add them here
242244
if (Array.isArray(this.options.plugins)) {
243-
this.options.plugins.forEach((p) => this.use(p))
245+
this.options.plugins.forEach((p) => this.use(p as never))
244246
}
245247

246-
return this as this & InferFeaturesFromPlugins<this, ClientOptions<this>["plugins"]>
248+
return this as this &
249+
InferFeaturesFromPlugins<this, Exclude<ClientOptions<this>["plugins"], undefined>>
247250
}
248251

249252
close() {
@@ -270,7 +273,7 @@ export class ReactotronImpl implements ReactotronCore {
270273

271274
// establish a connection to the server
272275
const protocol = secure ? "wss" : "ws"
273-
const socket = createSocket(`${protocol}://${host}:${port}`)
276+
const socket = createSocket!(`${protocol}://${host}:${port}`)
274277

275278
// fires when we talk to the server
276279
const onOpen = () => {
@@ -282,7 +285,7 @@ export class ReactotronImpl implements ReactotronCore {
282285

283286
const getClientIdPromise = getClientId || emptyPromise
284287

285-
getClientIdPromise(name).then((clientId) => {
288+
getClientIdPromise(name!).then((clientId) => {
286289
this.isReady = true
287290
// introduce ourselves
288291
this.send("client.intro", {
@@ -352,7 +355,7 @@ export class ReactotronImpl implements ReactotronCore {
352355
}
353356

354357
// this is ws style from require('ws') on node js
355-
if ("on" in socket && socket.on) {
358+
if ("on" in socket && socket.on!) {
356359
const nodeWebSocket = socket as WebSocket
357360
nodeWebSocket.on("open", onOpen)
358361
nodeWebSocket.on("close", onClose)
@@ -461,7 +464,7 @@ export class ReactotronImpl implements ReactotronCore {
461464
// here's how we're going to inject these in
462465
const inject = (key: string) => {
463466
// grab the function
464-
const featureFunction = plugin.features[key]
467+
const featureFunction = plugin.features![key]
465468

466469
// only functions may pass
467470
if (typeof featureFunction !== "function") {
@@ -494,20 +497,20 @@ export class ReactotronImpl implements ReactotronCore {
494497
onCustomCommand(config: CustomCommand | string, optHandler?: () => void): () => void {
495498
let command: string
496499
let handler: () => void
497-
let title: string
498-
let description: string
499-
let args: CustomCommandArg[]
500+
let title!: string
501+
let description!: string
502+
let args!: CustomCommandArg[]
500503

501504
if (typeof config === "string") {
502505
command = config
503-
handler = optHandler
506+
handler = optHandler!
504507
} else {
505508
command = config.command
506509
handler = config.handler
507510

508-
title = config.title
509-
description = config.description
510-
args = config.args
511+
title = config.title!
512+
description = config.description!
513+
args = config.args!
511514
}
512515

513516
// Validations
@@ -535,7 +538,7 @@ export class ReactotronImpl implements ReactotronCore {
535538
}
536539

537540
if (args) {
538-
const argNames = []
541+
const argNames = [] as string[]
539542

540543
args.forEach((arg) => {
541544
if (!arg.name) {
@@ -592,5 +595,5 @@ export function createClient<Client extends ReactotronCore = ReactotronCore>(
592595
options?: ClientOptions<Client>
593596
) {
594597
const client = new ReactotronImpl()
595-
return client.configure(options as unknown) as unknown as Client
598+
return client.configure(options as never) as unknown as Client
596599
}

lib/reactotron-core-client/src/serialize.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ function getFunctionName(fn: any): string {
4949
*
5050
* @param {any} source - The victim.
5151
*/
52-
function serialize(source, proxyHack = false) {
53-
const stack = []
54-
const keys = []
52+
function serialize(source: any, proxyHack = false) {
53+
const stack = [] as any[]
54+
const keys = [] as string[]
5555

5656
/**
5757
* Replace this object node with something potentially custom.
@@ -60,7 +60,7 @@ function serialize(source, proxyHack = false) {
6060
* @param {*} value - The value to replace.
6161
*/
6262
function serializer(replacer) {
63-
return function (this: any, key, value) {
63+
return function (this: any, key: string, value: any) {
6464
// slam dunks
6565
if (value === true) return true
6666

lib/reactotron-core-client/src/validate.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const isCreateSocketValid = (
55
createSocket: unknown
66
): createSocket is ClientOptions<ReactotronCore>["createSocket"] =>
77
typeof createSocket !== "undefined" && createSocket !== null
8-
const isHostValid = (host: string): boolean => typeof host === "string" && host && host !== ""
8+
const isHostValid = (host: string): boolean =>
9+
(typeof host === "string" && host && host !== "") as boolean
910
const isPortValid = (port: number): boolean =>
1011
typeof port === "number" && port >= 1 && port <= 65535
1112
const onCommandValid = (fn: (cmd: string) => any) => typeof fn === "function"
@@ -21,15 +22,15 @@ const validate = (options: ClientOptions<ReactotronCore>) => {
2122
throw new Error("invalid createSocket function")
2223
}
2324

24-
if (!isHostValid(host)) {
25+
if (!isHostValid(host!)) {
2526
throw new Error("invalid host")
2627
}
2728

28-
if (!isPortValid(port)) {
29+
if (!isPortValid(port!)) {
2930
throw new Error("invalid port")
3031
}
3132

32-
if (!onCommandValid(onCommand)) {
33+
if (!onCommandValid(onCommand!)) {
3334
throw new Error("invalid onCommand handler")
3435
}
3536
}

lib/reactotron-core-client/test/configure.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createClient } from "../src/reactotron-core-client"
2-
import WebSocket from "ws"
2+
import { WebSocket } from "ws"
33

44
const createSocket = (path) => new WebSocket(path)
55

lib/reactotron-core-client/test/plugin-clear.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { createClient, corePlugins } from "../src/reactotron-core-client"
22
import plugin from "../src/plugins/clear"
3-
import WebSocket from "ws"
3+
import { WebSocket } from "ws"
44

55
const createSocket = (path) => new WebSocket(path)
66

77
test("clears", () => {
88
const client: any = createClient({ createSocket })
9-
const results = []
9+
const results = [] as Array<{ type: string; payload: any }>
1010
client.send = (type, payload) => results.push({ type, payload })
1111
client.use(plugin())
1212
expect(client.plugins.length).toBe(corePlugins.length + 1)

lib/reactotron-core-client/test/plugin-logger.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { createClient, corePlugins } from "../src/reactotron-core-client"
22
import plugin from "../src/plugins/logger"
3-
import WebSocket from "ws"
3+
import { WebSocket } from "ws"
44

55
const createSocket = (path) => new WebSocket(path)
66

77
test("the 4 functions send the right data", () => {
88
const client: any = createClient({ createSocket })
9-
const results = []
9+
const results = [] as Array<{ type: string; payload: any }>
1010
client.send = (type, payload) => {
1111
results.push({ type, payload })
1212
}

lib/reactotron-core-client/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"allowJs": false,
55
"declaration": true,
66
"rootDir": ".",
7+
"strict": true,
78
"declarationDir": "dist/types",
89
"emitDeclarationOnly": true,
910
"emitDecoratorMetadata": true,

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24568,7 +24568,7 @@ __metadata:
2456824568
rollup-plugin-resolve: "npm:0.0.1-predev.1"
2456924569
ts-jest: "npm:^29.1.1"
2457024570
tslib: "npm:^2.6.2"
24571-
typescript: "npm:^4.9.5"
24571+
typescript: "npm:^5.1.3"
2457224572
ws: "npm:^8.14.2"
2457324573
languageName: unknown
2457424574
linkType: soft

0 commit comments

Comments
 (0)