Notes on enabling TypeScript in an existing Meteor app (June 2021)

I recently successfully enabled TypeScript for an existing Meteor project. I had about ~2000 tsc errors. I managed to bring it down to ~200 by disabling some very noisy rules:

"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false,

Most errors were easily fixable, either by adding some JSDoc, or by changing file extensions to .ts or .tsx and adding some type annotations. Some of those were non-trivial, and I think I managed to bypass the difficulty by using some very ugly type casting. I am new to TypeScript, I hope I find a better solution later.

One particular pain point, for a TypeScript beginner, is that Meteor.server is missing from the
@types/meteor package, so I had to cook something up to access Meteor methods
in server tests:

const methods = (
	Meteor as unknown as {server: {method_handlers: Record<string, Function>}}
).server.method_handlers;

Isn’t there a better way? Is the mostly undocumented and arcane Meteor.server the only way to test Meteor methods? Somebody already mentionned this particular issue earlier here

I had to create my own typings for the global variable Factory provided by the dburles:factory Meteor package. This is similar to issues others are having, for instance,

and

I had to update my linter (xojs/xo) to the latest version for better TypeScript support, then disabled a bunch of rules and fixed the rest, most of it automagically.

After all that, the tests, server, and client had errors. I had to remove all .js extensions on own imported TypeScript files (for simplicity, I removed it for all own files). It seems it would have worked fine if I had replaced those with .ts or .tsx where necessary instead. At least on Meteor’s side, because TypeScript then complains about not being able to resolve .ts/.tsx paths (see one of the relevant issues on GitHub). I tried to find another solution with the paths key in tsconfig.json but gave up. Do you have a better solution?

In regards to all the issues I had, I find the Meteor TypeScript guide a bit terse. Can someone improve it? It also fails to mention all the caveats listed in the PR implementing the official TypeScript compiler plugin.

And finally, would it make sense to have a TypeScript category on this forum?

3 Likes

Everything becomes simpler once you use Validated methods or equivalent.

2 Likes