How to Upgrade to TypeScript Without Anybody Noticing, Part 1
June 14th, 2019
This guide will show you how to upgrade to TypeScript without anybody noticing. Well, people might notice — what I really mean is that you won’t have to change your build at all. You’ll have the ability to get errors and completions in supported editors and to get errors on the command line from , the TypeScript compiler, but you won’t have to integrate TypeScript into your build.
Here’s what you’ll actually need to check in to source control:
- A TypeScript configuration file, .
- New dev dependencies from the package.
- A TypeScript declaration file to hold miscellaneous types.
This guide assumes that you have used TypeScript enough to:
- Have an editor set up to work with TypeScript.
- Have used npm to install a package.
- Know the basic syntax of type annotations — , , etc.
If you want to look at the package after the upgrade, you can run the following commands, or take a look at the branch on github:
Also make sure that you have TypeScript installed on the command line:
Your first step is to start with and change the settings in the that it produces. There are other ways to get started, but this gives you the most control. Run this command from the root of the project:
Here is what you should end up with, skipping the lines you don’t need to change:
- — Compile JS files.
- — Give errors on JS files.
- — Don’t emit downlevel code; just give errors.
- — Target the newest version of EcmaScript since we’re not emitting code anyway.
- — Target node’s module system since we’re not emitting code anyway.
- — Compile JSON files (if they’re small enough).
- — Don’t give the strictest possible errors.
A few notes:
- and should not actually matter since they have to do with generated downlevel code, but you’ll get some bogus errors if you use ES6 classes like or .
- is optional, but you’ll need it if your code ever s a JSON file, so that TypeScript will analyze it too.
You might want to use if, say, you only want to check your source and not your tests or scripts. Or you can use to give an explicit list of files to use, but this is annoying except for small projects.
OK, you’re all set. Run and make sure it prints out errors. Now open up files in your editor and make sure the same errors show up there. Below are the first few errors you should see:
You should be able to see the same errors when you open Makefile.js in your editor and look at lines 55 and 56.
Congratulations! You’ve done the only required part of the upgrade. You can check in and start getting benefits from TypeScript’s checking in the editor without changing anything else. Of course, there are a huge number of errors, hardly any of which are due to real bugs. So the next step is to start getting rid of incorrect errors and improving TypeScript’s knowledge of the code.
Here’s the commit.
Install @types packages.
Your first order of business is to install types for packages you use. This allows TypeScript to understand them, which makes it the easiest way to reduce the number of errors. Basically, if you have a dependency on some package, say, , and you see errors when you use it, you probably need a dev dependency on . The type definitions in give TypeScript a model of jquery that it can use to provide editor support, even though jquery was written before TypeScript existed.
Definitely Typed is the source for the packages in the namespace. Anybody can contribute new type definitions, but tons of packages already have type definitions, so you will probably find that most of your dependencies do too.
Here’s a good starting set for TypeScript-eslint-parser, although there are likely more available:
After the installation, these three types packages still didn’t work (although notice that we intentionally installed an old version of shelljs – more on that later):
They fail for different reasons, though. shelljs and es-lint-scope just don’t have types for a lot of their values. estree has all the correct types, but the types aren’t imported correctly.Part 2 shows how to fix these two problems.
At this point, you have types for some of your packages working and your own code checked by the TypeScript compiler. The next step is to fix compile errors in the rest of the package types, or to fix them in your own code. Or you can just ignore the errors and start using the TypeScript support in the editor.
Next up: Part 2, to learn about the various kinds of fixes.
- How do you stop tooth erosion
- What are some lesser known resource constraints
- How is Vietnam to work for Indians
- Whats wrong with frozen food
- What is similarity transformation
- How can Pure O OCD be cured?no_redirect=1
- Is Raipur a metro city
- Is sage supposed to be furry
- Why do some songs have instrument solos
- How do natural resources affect the environment
- Can LibreOffice successfully compete with Microsoft Office
- What are the benefits of ontology
- What do you expect everyday
- Can working memory be improved
- What is Tufts University known for
- What is your review of Toyota Yaris
- Why do my feet itch during sleep
- Why do buildings have windows
- How do people find their mentor