Skip to content

Commit

Permalink
Update Converting An App guides
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Dec 17, 2024
1 parent 4711a3e commit 5975c0c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
<!-- TODO: @gitKrystan Update this. -->

These directions are for converting an _existing_ Ember app to TypeScript. If you are starting a new app, you can use the directions in [Getting Started][].

## Enable TypeScript Features

### A Bit of a Hack
### Install TypeScript and Related Packages

Since `ember-cli` _currently_ has no flag to convert your project to TypeScript, we're going to use a bit of a hack and _temporarily_ install the legacy `ember-cli-typescript` addon to complete most of the migration:
See [Getting Started: Packages to Support TypeScript][packages] for descriptions of these packages.

```shell
ember install ember-cli-typescript@latest
npm add --save-dev typescript @tsconfig/ember
npm add --save-dev @types/qunit @types/rsvp
npm add --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser
npm remove @babel/plugin-proposal-decorators @babel/eslint-parser
```

The `ember-cli-typescript` addon will install _most_ of the necessary packages and create or update _most_ of the necessary files as described in [Getting Started][].
### Add TypeScript Configuration

Add a `tsconfig.json` file to the root of your project. Copy its contents from the [current output from the Ember CLI blueprints][tsconfig.json].

### Set Up TypeScript for EmberData

You can then immediately remove the `ember-cli-typescript` dependency and follow the rest of this guide.
<!-- FIXME: @gitKrystan Link -->

### Manually Enable TypeScript Transpilation
### Enable TypeScript Transpilation for Builds

To enable TypeScript transpilation in your app, simply add the corresponding configuration for Babel to your `ember-cli-build.js` file.

```javascript {data-filename="ember-cli-build.js" data-diff="+2"}
const app = new EmberApp(defaults, {
'ember-cli-babel': { enableTypeScriptTransform: true },
});
```javascript {data-filename="ember-cli-build.js" data-diff="+3"}
module.exports = function (defaults) {
const app = new EmberApp(defaults, {
'ember-cli-babel': { enableTypeScriptTransform: true },
// ...
});

return app.toTree();
};
```

### Manually Add `lint:types` Script
### Enable Type Checking in CI

To easily check types with the command line, add the `lint:types` script as shown [here][lint-types].

The default `lint` script generated by Ember CLI will include the `lint:types` script automatically.

### Manually Force Blueprint Generators to Use TypeScript
### Configure Blueprint Generators to Use TypeScript

With the following configuration, project files will be generated with `.ts` extensions instead of `.js`:

Expand All @@ -43,14 +53,35 @@ With the following configuration, project files will be generated with `.ts` ext
}
```

### Manually Set Up `@typescript-eslint`

```shell
npm add --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser
```js {data-filename="config/ember-cli-update.json" data-diff="+12"}
{
// ...
"packages": [
{
"name": "ember-cli",
// ...
"blueprints": [
{
// ...
"options": [
// ...
"--typescript"
]
}
]
}
]
}
```

### Configure ESLint

Then, update your `.eslintrc.js` to include the [current output from the Ember CLI blueprints][eslintrc]. You might consider using ESLint [overrides][] configuration to separately configure your JavaScript and TypeScript files during the migration.

### Add Initial Type Declarations

Add types for your `config/environment.js` file by creating a type declaration file at `app/config/environment.d.ts`. You can find an example file in the [current output from the Ember CLI blueprints][environment.d.ts].

## Migrate Existing Code to TypeScript

Once you have set up TypeScript following the guides above, you can begin to migrate your files incrementally by changing their extensions from `.js` to `.ts`. Fortunately, TypeScript allows for gradual typing. This means that you can use TypeScript and JavaScript files interchangeably, so you can convert your app piecemeal.
Expand Down Expand Up @@ -85,23 +116,29 @@ In general, we recommend migrating to Octane idioms before, or in conjunction wi

## ember-cli-typescript

If you're migrating from `ember-cli-typescript`, particularly an older version, to Ember's out-of-the-box TypeScript support, you may also need to update your `tsconfig.json`. Current versions of `ember-cli-typescript` generate the correct config at installation. You do _not_ need `ember-cli-typescript` installed for new apps or addons.
The `ember-cli-typescript` package was used to add TypeScript support to Ember apps before Ember's native TypeScript support was available. You do _not_ need `ember-cli-typescript` installed for new apps or addons.

If you're migrating from `ember-cli-typescript` to Ember's native TypeScript support, most of your existing configuration will still be relevant. Just read through the steps of this guide and ensure that your config matches the expected config as described above.

<!-- Internal links -->

[getting started]: ../../getting-started/
[legacy]: ../../additional-resources/legacy/
[packages]: ../../getting-started/#toc_packages-to-support-typescript
[strictness]: ../../additional-resources/faq/#toc_strictness

<!-- External links -->

[allowJs]: https://www.typescriptlang.org/tsconfig/#allowJs
[any]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any
[dts]: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
[environment.d.ts]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/app/config/environment.d.ts
[eslintrc]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/.eslintrc.js
[global.d.ts]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/types/global.d.ts
[lint-types]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/package.json
[JSDoc]: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#handbook-content
[overrides]: https://eslint.org/docs/latest/use/configure/configuration-files#configuration-based-on-glob-patterns
[ts-check]: https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html#ts-check
[ts-expect-error]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html
[tsconfig.json]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/tsconfig.json
[unknown]: https://www.typescriptlang.org/docs/handbook/2/functions.html
6 changes: 6 additions & 0 deletions guides/release/typescript/core-concepts/ember-data.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Adding EmberData Types to an Existing TypeScript App

```shell
npx warp-drive retrofit types@beta
```

<!-- TODO: @gitKrystan Rewrite this. -->

In this section, we cover how to use TypeScript effectively with specific EmberData APIs (anything you'd find under the `@ember-data` package namespace).
Expand Down
4 changes: 4 additions & 0 deletions guides/release/typescript/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Project files will be generated with `.ts` extensions instead of `.js`.
In addition to the usual packages added with `ember new`, the following packages will be added at their current "latest" value:

- `typescript` – tooling to support TypeScript type checking and compilation.
<!--
TODO: Uncomment this line when we add Glint docs
- `@glint/*` – a set of packages to support type-checking in templates.
-->
- `@tsconfig/ember` – a shared TypeScript configuration for Ember apps.
- `@typescript-eslint/*` – ESLint support for TypeScript.
- `@types/qunit` - TypeScript type definitions for QUnit.
Expand Down

0 comments on commit 5975c0c

Please sign in to comment.