Upgrading from 1.x
There are a number of important changes between ember-cli-typescript v1 and v2, which mean the upgrade process is straightforward but specific:
Update ember-cli-babel. Fix any problems introduced during the upgrade.
Update ember-decorators. Fix any problems introduced during the upgrade.
Update ember-cli-typescript. Follow the detailed upgrade guide below to fix discrepancies between Babel and TypeScript's compiled output.
If you deviate from this order, you are likely to have a much more difficult time upgrading!
Update ember-cli-babel
ember-cli-typescript requires ember-cli-babel at version 7.1.0 or above, which requires ember-cli 2.13 or above. It also requires @babel/core 7.2.0 or higher.
The recommended approach here is to deduplicate existing installations of the dependency, remove and reinstall ember-cli-babel to make sure that all its transitive dependencies are updated to the latest possible, and then to deduplicate again.
If using yarn:
If using npm:
Note: If you are also using ember-decorators—and specifically the babel-transform that gets added with it—you will need update @ember-decorators/babel-transforms as well (anything over 3.1.0 should work):
Update ember-decorators
If you're on a version of Ember before 3.10, follow the same process of deduplication, reinstallation, and re-deduplication as described for ember-cli-babel above for ember-decorators. This will get you the latest version of ember-decorators and, importantly, its @ember-decorators/babel-transforms dependency.
Update ember-cli-typescript
Now you can simply ember install
the dependency like normal:
Note: To work properly, starting from v2, ember-cli-typescript must be declared as a dependency
, not a devDependency
for addons. With ember install
this migration would be automatically handled for you.
If you choose to make the upgrade manually with yarn or npm, here are the steps you need to follow:
Remove ember-cli-typescript from your
devDependencies
.With yarn:
With npm:
Install the latest of ember-cli-typescript as a
dependency
:With yarn:
With npm:
Run
ember generate
:
Account for addon build pipeline changes
Since we now integrate in a more traditional way into Ember CLI's build pipeline, there are two changes required for addons using TypeScript.
Addons can no longer use
.ts
inapp
, because an addon'sapp
directory gets merged with and uses the host's (i.e. the other addon or app's) preprocessors, and we cannot guarantee the host has TS support. Note that.ts
will continue to work for in-repo addons because the app build works with the host's (i.e. the app's, not the addon's) preprocessors.Similarly, apps must use
.js
to override addon defaults inapp
, since the different file extension means apps no longer consistently "win" over addon versions (a limitation of how Babel + app merging interact).
Account for TS → Babel issues
ember-cli-typescript v2 uses Babel to compile your code, and the TypeScript compiler only to check your code. This makes for much faster builds, and eliminates the differences between Babel and TypeScript in the build output that could cause problems in v1. However, because of those differences, you’ll need to make a few changes in the process of upgrading.
Any place where a type annotation overrides a getter
Fields like
element
,disabled
, etc. as annotated defined on a subclass ofComponent
and (correctly) not initialized to anything, e.g.:This breaks because
element
is a getter onComponent
. This declaration then shadows the getter declaration on the base class and stomps it toundefined
(effectivelyObject.defineProperty(this, 'element', void 0)
. (It would be nice to usedeclare
here, but that doesn't work: you cannot usedeclare
with a getter in a concrete subclass.)Two solutions:
Annotate locally (slightly more annoying, but less likely to troll you):
Use a local getter:
Notably, this is not a problem for Glimmer components, so migrating to Octane will also help!
const enum
is not supported at all. You will need to replace all uses ofconst enum
with simplyenum
or constants.Using ES5 getters or setters with
this
type annotations is not supported through at least Babel 7.3. However, they should also be unnecessary with ES6 classes, so you can simply remove thethis
type annotation.Trailing commas after rest function parameters (
function foo(...bar[],) {}
) are disallowed by the ECMAScript spec, so Babel also disallows them.Re-exports of types have to be disambiguated to be types, rather than values. Neither of these will work:
In both cases, Babel attempts to emit a value export, not just a type export, and fails because there is no actual value to emit. You can do this instead as a workaround:
Last updated