This repository was archived by the owner on Mar 5, 2024. It is now read-only.
fix: use type narrowing & more precise types #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This narrows down the RPC types to what the comment specifies, effectively making it obsolete and actually enforced.
Literal values are their own types, which means we can specify these unions of literals just fine.
The
Callbacksignature is also very naïve.anyis not a useful type, but rather just a way to turn off the type system when there is a lack of understanding of what's going on. A callback taking JSON data of an unknown shape should instead be specified as takingunknown, which means the callback itself will have to ascertain what the shape of the data is, via type guards.unknownis effectively a version ofanythat cannot be passed to something requiring a more specific type, which means it's a type-safe way to refer to data that is of unknown shape.Further work on the RPC functions should probably be done, with a union of possible commands being created (which would also then remove the need for the stringly typed
dataparameter). In general, the code, small as it is, is full of places where thinking about the possible values of things and actually nailing these down could be useful.It's clear, for example, that there is a valid set of inputs and a shape to
jmsgin the code, except the expectation is never validated or otherwise established.JSON.parseis a nebulous function to use for these things, as it returnsany. This illustrates the issue with lettinganyinto the codebase. If it returnedunknown, the code would instead have to actually establish that it's dealing with the correct data.anyis also used forMINIMA_PARAMSin place of a more precise type. I believe this is likely the reason for the nonsensicalreturn MINIMA_PARAMS[paramname]in a function that returnsvoid(form.params,minima.ts:555).