Skip to content

Commit e675d8f

Browse files
gitKrystanNullVoxPopulief4
authored
Update TS Guides for Native Types (#2064)
* WIP: Native Types Bonanza * Fix TS gotcha docs * Update addons section * Update Converting An App guides * Update EmberData TS guides * Update tsconfig types docs with latest info * Fix lint * eslint.config.mjs update Co-authored-by: NullVoxPopuli <[email protected]> --------- Co-authored-by: NullVoxPopuli <[email protected]> Co-authored-by: Edward Faulkner <[email protected]>
1 parent 8fcae19 commit e675d8f

File tree

7 files changed

+227
-223
lines changed

7 files changed

+227
-223
lines changed

.local.dic

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ autotracking
1818
aXe
1919
backticks
2020
beforeEach
21+
BelongsTo
2122
blockquote
2223
blogPost
2324
bookmarklet
@@ -101,6 +102,7 @@ hackable
101102
HammerJS
102103
hardcode
103104
hashchange
105+
HasMany
104106
Hm
105107
Hoc
106108
htmlbars
@@ -245,6 +247,7 @@ VoiceOver
245247
voilà
246248
Voilà
247249
vscode-glimmer-syntax
250+
WarpDrive
248251
websocket
249252
working-with-html-css-and-javascript
250253
yay

guides/release/typescript/additional-resources/gotchas.md

-31
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ For examples, see:
3333

3434
- [Service][service] registry
3535
- [Controller][controller] registry
36-
- EmberData [Model][model] registry
37-
- EmberData [Transform][transform] registry
38-
- EmberData [Serializer][serializers-and-adapters] registry
39-
- EmberData [Adapter][serializers-and-adapters] registry
4036

4137
## Decorators
4238

@@ -125,43 +121,16 @@ export default class MyRoute extends Route {
125121
}
126122
```
127123

128-
## Fixing the EmberData `error TS2344` problem
129-
130-
If you're developing an Ember app or addon and _not_ using EmberData (and accordingly not even have the EmberData types installed), you may see an error like this and be confused:
131-
132-
```text
133-
node_modules/@types/ember-data/index.d.ts(920,56): error TS2344: Type 'any' does not satisfy the constraint 'never'.
134-
```
135-
136-
This happens because the types for Ember's _test_ tooling includes the types for EmberData because the `this` value in several of Ember's test types can include a reference to the EmberData `Store` class.
137-
138-
**The fix:** add a declaration like this in a new file named `ember-data.d.ts` in your `types` directory:
139-
140-
```typescript {data-filename="types/ember-data.d.ts"}
141-
declare module 'ember-data/types/registries/model' {
142-
export default interface ModelRegistry {
143-
[key: string]: unknown;
144-
}
145-
}
146-
```
147-
148-
This works because (a) we include things in your types directory automatically and (b) TypeScript will merge this module and interface declaration with the main definitions for EmberData from DefinitelyTyped behind the scenes.
149-
150-
If you're developing an addon and concerned that this might affect consumers, it won't. Your types directory will never be referenced by consumers at all!
151-
152124
<!-- Internal links -->
153125

154126
[controller]: ../../core-concepts/routing/#toc_controller-injections-and-lookups
155127
[get-set]: ../../additional-resources/legacy/#toc_classic-get-or-set-methods
156128
[model-attr]: ../../core-concepts/ember-data/#toc_attr
157129
[model-belongsto]: ../../core-concepts/ember-data/#toc_belongsto
158130
[model-hasmany]: ../../core-concepts/ember-data/#toc_hasMany
159-
[model]: ../../core-concepts/ember-data/#toc_models
160131
[owner-lookup]: https://api.emberjs.com/ember/release/classes/Owner/methods/lookup?anchor=lookup
161-
[serializers-and-adapters]: ../../core-concepts/ember-data/#toc_serializers-and-adapters
162132
[service]: ../../core-concepts/services/#toc_using-services
163133
[signature]: ../../core-concepts/invokables/#toc_signature-basics
164-
[transform]: ../../core-concepts/ember-data/#toc_transforms
165134

166135
<!-- External links -->
167136

guides/release/typescript/application-development/addons.md

+7-13
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,19 @@ Project files will be generated with `.ts` extensions instead of `.js`.
1818

1919
In addition to the usual packages added with `ember addon`, the following packages will be added at their current "latest" value:
2020

21-
- `typescript`
22-
- `@tsconfig/ember`
23-
- `@typescript-eslint/*`
24-
- `@types/ember`
25-
- `@types/ember-data`
26-
- `@types/ember__*``@types/ember__object` for `@ember/object`, etc.
27-
- `@types/ember-data__*``@types/ember-data__model` for `@ember-data/model`, etc.
28-
- `@types/qunit`
29-
- `@types/rsvp`
30-
31-
The `typescript` package provides tooling to support TypeScript type checking and compilation. The `@types` packages from [DefinitelyTyped][] provide TypeScript type definitions for all of the Ember and EmberData modules.
21+
- `typescript` – tooling to support TypeScript type checking and compilation.
22+
- `@tsconfig/ember` – a shared TypeScript configuration for Ember apps.
23+
- `@typescript-eslint/*` – ESLint support for TypeScript.
24+
- `@types/qunit` - TypeScript type definitions for QUnit.
25+
- `@types/rsvp` - TypeScript type definitions for RSVP.
26+
- `@warp-drive/core-types` - shared core types, type utilities and constants for the WarpDrive and EmberData packages.
3227

3328
<div class="cta">
3429
<div class="cta-note">
3530
<div class="cta-note-body">
3631
<div class="cta-note-heading">Zoey says...</div>
3732
<div class="cta-note-message">
38-
Ember also publishes its own native types compiled directly from its source code, as described <a href="https://blog.emberjs.com/stable-typescript-types-in-ember-5-1/">in this blog post</a>. For now, we continue to use the <code>@types</code> packages by default for the sake of compatibility with EmberData, because EmberData is not yet compatible with Ember's native official types. However, if you do not use EmberData, we <i>highly</i> recommend following the instructions in that blog post to switch to the native types, which are guaranteed to always be 100% correct and 100% up to date!
33+
Ember and EmberData publish their own native types compiled directly from their source code, so you do not need to install any <code>@types/ember</code> or <code>@types/ember-data</code> packages. These packages should be considered legacy, are only lightly maintained, and will conflict with the native types.
3934
</div>
4035
</div>
4136
<img src="/images/mascots/zoey.png" role="presentation" alt="">
@@ -164,7 +159,6 @@ declare module 'addon/templates/*' {
164159

165160
<!-- External links -->
166161

167-
[DefinitelyTyped]: https://github.com/DefinitelyTyped/DefinitelyTyped
168162
[dts]: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
169163
[ember-addon]: https://cli.emberjs.com/release/writing-addons/
170164
[glint]: https://typed-ember.gitbook.io/glint/

guides/release/typescript/application-development/configuration.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ If you use the `--typescript` flag when generating your Ember app, we generate a
1111
"my-app/tests/*": ["tests/*"],
1212
"my-app/*": ["app/*"],
1313
"*": ["types/*"]
14-
}
14+
},
15+
"types": [
16+
"ember-source/types",
17+
"ember-data/unstable-preview-types",
18+
// ...more ember-data types...
19+
"@warp-drive/core-types/unstable-preview-types"
1520
}
1621
}
1722
```
@@ -38,8 +43,8 @@ To enable TypeScript sourcemaps, you'll need to add the corresponding configurat
3843

3944
```javascript {data-filename="ember-cli-build.js" data-diff="+3"}
4045
const app = new EmberApp(defaults, {
41-
'ember-cli-babel': { enableTypeScriptTransform: true },
42-
babel: { sourceMaps: 'inline' },
46+
"ember-cli-babel": { enableTypeScriptTransform: true },
47+
babel: { sourceMaps: "inline" },
4348
});
4449
```
4550

guides/release/typescript/application-development/converting-an-app.md

+58-19
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,47 @@ These directions are for converting an _existing_ Ember app to TypeScript. If yo
22

33
## Enable TypeScript Features
44

5-
### A Bit of a Hack
5+
### Install TypeScript and Related Packages
66

7-
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:
7+
See [Getting Started: Packages to Support TypeScript][packages] for descriptions of these packages.
88

99
```shell
10-
ember install ember-cli-typescript@latest
10+
npm add --save-dev typescript @tsconfig/ember
11+
npm add --save-dev @types/qunit @types/rsvp
12+
npm add --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser
13+
npm remove @babel/plugin-proposal-decorators @babel/eslint-parser
1114
```
1215

13-
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][].
16+
### Add TypeScript Configuration
17+
18+
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].
1419

15-
You can then immediately remove the `ember-cli-typescript` dependency and follow the rest of this guide.
20+
### Set Up TypeScript for EmberData
1621

17-
### Manually Enable TypeScript Transpilation
22+
Follow the instructions in the [EmberData Typescript Guides][ED-ts-guides].
23+
24+
### Enable TypeScript Transpilation for Builds
1825

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

21-
```javascript {data-filename="ember-cli-build.js" data-diff="+2"}
22-
const app = new EmberApp(defaults, {
23-
'ember-cli-babel': { enableTypeScriptTransform: true },
24-
});
28+
```javascript {data-filename="ember-cli-build.js" data-diff="+3"}
29+
module.exports = function (defaults) {
30+
const app = new EmberApp(defaults, {
31+
"ember-cli-babel": { enableTypeScriptTransform: true },
32+
// ...
33+
});
34+
35+
return app.toTree();
36+
};
2537
```
2638

27-
### Manually Add `lint:types` Script
39+
### Enable Type Checking in CI
2840

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

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

33-
### Manually Force Blueprint Generators to Use TypeScript
45+
### Configure Blueprint Generators to Use TypeScript
3446

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

@@ -41,13 +53,34 @@ With the following configuration, project files will be generated with `.ts` ext
4153
}
4254
```
4355

44-
### Manually Set Up `@typescript-eslint`
45-
46-
```shell
47-
npm add --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser
56+
```js {data-filename="config/ember-cli-update.json" data-diff="+12"}
57+
{
58+
// ...
59+
"packages": [
60+
{
61+
"name": "ember-cli",
62+
// ...
63+
"blueprints": [
64+
{
65+
// ...
66+
"options": [
67+
// ...
68+
"--typescript"
69+
]
70+
}
71+
]
72+
}
73+
]
74+
}
4875
```
4976

50-
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.
77+
### Configure ESLint
78+
79+
Then, update your `eslint.config.mjs` 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.
80+
81+
### Add Initial Type Declarations
82+
83+
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].
5184

5285
## Migrate Existing Code to TypeScript
5386

@@ -83,23 +116,29 @@ In general, we recommend migrating to Octane idioms before, or in conjunction wi
83116

84117
## ember-cli-typescript
85118

86-
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.
119+
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.
120+
121+
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.
87122

88123
<!-- Internal links -->
89124

125+
[ED-ts-guides]: ../../core-concepts/ember-data/#toc_adding-emberdata-types-to-an-existing-typescript-app
90126
[getting started]: ../../getting-started/
91127
[legacy]: ../../additional-resources/legacy/
128+
[packages]: ../../getting-started/#toc_packages-to-support-typescript
92129
[strictness]: ../../additional-resources/faq/#toc_strictness
93130

94131
<!-- External links -->
95132

96133
[allowJs]: https://www.typescriptlang.org/tsconfig/#allowJs
97134
[any]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any
98135
[dts]: https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
99-
[eslintrc]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/.eslintrc.js
136+
[environment.d.ts]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/app/config/environment.d.ts
137+
[eslintrc]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/eslint.config.mjs
100138
[lint-types]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/package.json
101139
[JSDoc]: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#handbook-content
102140
[overrides]: https://eslint.org/docs/latest/use/configure/configuration-files#configuration-based-on-glob-patterns
103141
[ts-check]: https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html#ts-check
104142
[ts-expect-error]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html
143+
[tsconfig.json]: https://github.com/ember-cli/editor-output/blob/stackblitz-app-output-typescript/tsconfig.json
105144
[unknown]: https://www.typescriptlang.org/docs/handbook/2/functions.html

0 commit comments

Comments
 (0)