Skip to content

Commit 31588e7

Browse files
authored
chore: fix typescript declarations (adopted-ember-addons#445)
* chore: fix typescript declarations * fix * remove attributes registry * fix typo
1 parent d74d95d commit 31588e7

File tree

4 files changed

+40
-79
lines changed

4 files changed

+40
-79
lines changed

README.md

+17-49
Original file line numberDiff line numberDiff line change
@@ -580,65 +580,33 @@ import AnimalSerializer from './animal';
580580
export default AnimalSerializer;
581581
```
582582

583-
## Typescript
583+
## TypeScript
584584

585-
To use with typescript, define the following files in your project:
585+
TypeScript declarations are included out of the box. For additional type safety for `createFragment`, `push`, etc. you can index your fragment classes in the `FragmentRegistry`:
586586

587587
```typescript
588-
// types/ember-data-model-fragments/types/registries/fragment.d.ts
588+
// app/models/address.ts
589589
import Fragment from 'ember-data-model-fragments/fragment';
590-
591-
import Product from '<your-app-name>/models/product';
592-
593-
/**
594-
* Define all your fragments here.
595-
*/
596-
export default interface FragmentRegistry {
597-
[key: string]: Fragment;
598-
'product': Product;
599-
}
600-
```
601-
602-
And the following one which will help you validate entries when doing a `createFragment`, `push` on a fragmentArray, etc...
603-
604-
```typescript
605-
// types/ember-data-model-fragments/types/registries/fragment-attributes.d.ts
606-
import Fragment from 'ember-data-model-fragments/fragment';
607-
608-
import { ProductAttributes } from '<your-app-name>/models/product';
609-
610-
/**
611-
* Define all your fragments attributes here.
612-
*/
613-
export default interface FragmentRegistry {
614-
[key: string]: Record<string, any>;
615-
'product': ProductAttributes;
616-
}
617-
```
618-
619-
Those attributes can be implemented like so:
620-
621-
```typescript
622-
// app/models/product.ts
623590
import { attr } from '@ember-data/model';
624-
import Fragment from 'ember-data-model-fragments/fragment';
625591

626-
import Order from './order';
592+
export default class AddressFragment extends Fragment {
593+
@attr('string')
594+
declare street: string;
627595

628-
export interface ProductAttributes {
629-
name: string;
630-
orders: Array<Order>;
631-
}
596+
@attr('string')
597+
declare city: string;
598+
599+
@attr('string')
600+
declare region: string;
632601

633-
export default class Product
634-
extends Fragment
635-
implements ProductAttributes
636-
{
637602
@attr('string')
638-
declare name: string;
603+
declare country: string;
604+
}
639605

640-
@fragmentArray('order')
641-
declare smartActions: Array<Order>;
606+
declare module 'ember-data-model-fragments/types/registries/fragment' {
607+
export default interface FragmentRegistry {
608+
address: AddressFragment;
609+
}
642610
}
643611
```
644612

attributes.d.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import Model from '@ember-data/model';
22
import EmberArray from '@ember/array';
33
import ComputedProperty from '@ember/object/computed';
4+
import Fragment from 'ember-data-model-fragments/fragment';
45
import FragmentRegistry from 'ember-data-model-fragments/types/registries/fragment';
5-
import FragmentAttributesRegistry from 'ember-data-model-fragments/types/registries/fragment-attributes';
66
// eslint-disable-next-line ember/use-ember-data-rfc-395-imports
77
import TransformRegistry from 'ember-data/types/registries/transform';
88

9-
interface FragmentArray<FragmentType extends keyof FragmentRegistry> extends EmberArray<FragmentType> {
10-
addFragment(fragment: FragmentRegistry[FragmentType]): FragmentRegistry[FragmentType];
11-
removeFragment(fragment: FragmentRegistry[FragmentType]): FragmentRegistry[FragmentType];
12-
createFragment(attributes: FragmentAttributesRegistry[FragmentType]): FragmentRegistry[FragmentType];
9+
interface FragmentArray<T extends Fragment> extends EmberArray<T> {
10+
addFragment(fragment: T): T;
11+
removeFragment(fragment: T): T;
12+
createFragment(attributes?: Partial<T>): T;
1313
}
1414

15-
interface FragmentOptions<FragmentType extends keyof FragmentRegistry> {
15+
interface FragmentOptions<K extends keyof FragmentRegistry> {
1616
polymorphic?: boolean;
17-
typeKey?: string | ((data: FragmentAttributesRegistry[FragmentType], owner: Model) => string);
18-
defaultValue?: () => FragmentAttributesRegistry[FragmentType] | FragmentAttributesRegistry[FragmentType];
17+
typeKey?: string | ((data: FragmentRegistry[K], owner: Model) => string);
18+
defaultValue?: Partial<FragmentRegistry[K]> | (() => Partial<FragmentRegistry[K]>);
1919
}
2020

21-
type TransformType<FragmentType extends keyof TransformRegistry> = ReturnType<
22-
TransformRegistry[FragmentType]['deserialize']
21+
type TransformType<K extends keyof TransformRegistry> = ReturnType<
22+
TransformRegistry[K]['deserialize']
2323
>;
2424

25-
export function fragment<FragmentType extends keyof FragmentRegistry>(
26-
type: FragmentType,
27-
options?: FragmentOptions<FragmentType>,
28-
): ComputedProperty<FragmentRegistry[FragmentType]>;
29-
export function fragmentArray<FragmentType extends keyof FragmentRegistry>(
30-
type: FragmentType,
31-
options?: FragmentOptions<FragmentType>,
32-
): ComputedProperty<FragmentArray<FragmentType>>;
33-
export function array<FragmentType extends keyof TransformRegistry>(): ComputedProperty<TransformRegistry[FragmentType]>;
25+
export function fragment<K extends keyof FragmentRegistry>(
26+
type: K,
27+
options?: FragmentOptions<K>,
28+
): ComputedProperty<FragmentRegistry[K]>;
29+
export function fragmentArray<K extends keyof FragmentRegistry>(
30+
type: K,
31+
options?: FragmentOptions<K>,
32+
): ComputedProperty<FragmentArray<FragmentRegistry[K]>>;
33+
export function array<T extends keyof TransformRegistry>(): ComputedProperty<TransformRegistry[T]>;
3434

3535
export function fragmentOwner(): ComputedProperty<Model>;

ext.d.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import FragmentRegistry from 'ember-data-model-fragments/types/registries/fragment';
2-
import FragmentAttributesRegistry from 'ember-data-model-fragments/types/registries/fragment-attributes';
32
import EmberStore from '@ember-data/store';
43

54
export class Store extends EmberStore {
6-
createFragment<FragmentType extends keyof FragmentRegistry>(
7-
type: FragmentType,
8-
attributes: FragmentAttributesRegistry[FragmentType],
9-
): FragmentRegistry[FragmentType];
5+
createFragment<K extends keyof FragmentRegistry>(
6+
type: K,
7+
attributes?: Partial<FragmentRegistry[K]>,
8+
): FragmentRegistry[K];
109
}

types/registries/fragment-attributes.d.ts

-6
This file was deleted.

0 commit comments

Comments
 (0)