Open psvenk opened 3 years ago
Here is a mockup of type definitions that I wrote on June 21. This has not been tested on the actual code; I had written it only to give an initial idea of what type definitions would look like.
Another advantage of type safety is that we can share some type definitions between the frontend and the backend so that they act as a contract between the two, making sure that the API is consistent (at least in the type of data to expect in each place). This should also alleviate confusion surrounding the naming of tableData
, currentTableData
, currentTerm
, etc.
Because we are rewriting scrape.js
anyway for #184, @notrodes suggested that we simply rewrite it in TypeScript instead of using JSDoc.
Gradually move the codebase to have (optional) type checking, in the form of JSDoc comments in the source code that can be checked by
tsc
(the TypeScript compiler). Such an arrangement confers the advantages of type safety without introducing a build step because the source code will continue to be executable JavaScript. Documentation about this feature: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.htmlThe first step is adjusting the codebase (backend and frontend) to type check without adding many type annotations. Currently, running
tsc
gives failures because there are places where we abuse JavaScript's flexibility by assigning a number or a string to a variable depending on context, for example.Next, we would need to write type definitions for the data structures that we use (these can be in separate
.ts
files that are included in.js
files via@include
comments), such astableData
andcurrentTableData
, and add type annotations so that TypeScript can type-check more of the code (otherwise, the default is for TypeScript to assume that every variable has typeany
, which nearly disables type checking). Eventually, we may want to enable thenoImplicitAny
option which causes TypeScript to require type annotations wherever type inference cannot be performed. Some work on this step has been done in pull request #128.