Skip to content

Commit 4e52cc7

Browse files
authored
Merge branch 'master' into de-DS-docs
2 parents 8e267c2 + 9d17480 commit 4e52cc7

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

docs/ember-data/models.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,17 @@ export default class User extends Model {
5858
}
5959
```
6060

61-
## `@belongsTo`
61+
## Relationships
62+
63+
Relationships between models in Ember Data rely on importing the related models, like `import User from './user';`. This, naturally, can cause a recursive loop, as `/app/models/post.ts` imports `User` from `/app/models/user.ts`, and `/app/models/user.ts` imports `Post` from `/app/models/post.ts`. Recursive importing triggers an [`import/no-cycle`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-cycle.md) error from eslint.
64+
65+
To avoid these errors, use of [type-only imports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html), available since TypeScript 3.8:
66+
67+
```ts
68+
import type User from './user';
69+
```
70+
71+
### `@belongsTo`
6272

6373
The type returned by the `@belongsTo` decorator depends on whether the relationship is `{ async: true }` \(which it is by default\).
6474

@@ -68,9 +78,9 @@ The type returned by the `@belongsTo` decorator depends on whether the relations
6878
So, for example, you might define a class like this:
6979

7080
```typescript
71-
import Model, { belongsTo, AsyncBelongsTo } from '@ember-data/model';
72-
import User from './user';
73-
import Site from './site';
81+
import Model, { belongsTo, type AsyncBelongsTo } from '@ember-data/model';
82+
import type User from './user';
83+
import type Site from './site';
7484

7585
export default class Post extends Model {
7686
@belongsTo('user')
@@ -88,7 +98,7 @@ These are _type_-safe to define as always present, that is to leave off the `?`
8898

8999
Note, however, that this type-safety is not a guarantee of there being no runtime error: you still need to uphold the contract for non-async relationships \(that is: loading the data first, or side-loading it with the request\) to avoid throwing an error!
90100

91-
## `@hasMany`
101+
### `@hasMany`
92102

93103
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }` \(which it is by default\).
94104

@@ -98,9 +108,9 @@ The type returned by the `@hasMany` decorator depends on whether the relationshi
98108
So, for example, you might define a class like this:
99109

100110
```typescript
101-
import Model, { hasMany, AsyncHasMany, SyncHasMany } from '@ember-data/model';
102-
import Comment from './comment';
103-
import User from './user';
111+
import Model, { hasMany, type AsyncHasMany, type SyncHasMany } from '@ember-data/model';
112+
import type Comment from './comment';
113+
import type User from './user';
104114

105115
export default class Thread extends Model {
106116
@hasMany('comment')

ts/blueprints/ember-cli-typescript/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const path = require('path');
66
const APP_DECLARATIONS = `import Ember from 'ember';
77
88
declare global {
9+
// Prevents ESLint from "fixing" this via its auto-fix to turn it into a type
10+
// alias (e.g. after running any Ember CLI generator)
11+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
912
interface Array<T> extends Ember.ArrayPrototypeExtensions<T> {}
1013
// interface Function extends Ember.FunctionPrototypeExtensions {}
1114
}

0 commit comments

Comments
 (0)