Ember-cli-typescript: Provide convenience types for hasMany and belongsTo relationships

Created on 9 Dec 2020  路  5Comments  路  Source: typed-ember/ember-cli-typescript

Which package(s) does this enhancement pertain to?

  • [ ] @types/ember
  • [ ] @types/ember__string
  • [ ] @types/ember__polyfills
  • [ ] @types/ember__object
  • [ ] @types/ember__utils
  • [ ] @types/ember__array
  • [ ] @types/ember__engine
  • [ ] @types/ember__debug
  • [ ] @types/ember__runloop
  • [ ] @types/ember__error
  • [ ] @types/ember__controller
  • [ ] @types/ember__component
  • [ ] @types/ember__routing
  • [ ] @types/ember__application
  • [ ] @types/ember__test
  • [ ] @types/ember__test-helpers
  • [ ] @types/ember__service
  • [x] @types/ember-data
  • [ ] @types/rsvp
  • [ ] Other
  • [ ] I don't know

Please write a user story for this feature

As a developer, I want types importable from @ember-data/model that can be used to type model properties that represent async hasMany and belongsTo relationships.

These currently require importing from ember-data and using the somewhat esoteric PromiseManyArray (for hasMany) and PromiseObject (for belongsTo), e.g.

import Model, { hasMany, belongsTo } from `@ember-data/model`;
import DS from 'ember-data';

export default class MyModel extends Model {
  @hasMany('foo') foos!: DS.PromiseManyArray<Foo>;
  @belongsTo('bar') bar!: DS.PromiseObject<Bar> & Bar;
}

It would be nicer to have:

import Model, { hasMany, HasMany, belongsTo, BelongsTo } from `@ember-data/model`;

export default class MyModel extends Model {
  @hasMany('foo') foos!: HasMany<Foo>;
  @belongsTo('bar') bar!: BelongsTo<Bar>;
}
enhancement core

Most helpful comment

All 5 comments

Many developers type relationships like this:

  @belongsTo('bar') bar!: Bar;
  @hasMany('foo') foos!: Foo[];

But this is problematic because it does not expose .meta, .reload(), etc.

The worst part of that wishful typing is that it assumes that this.foos[0] is possible, which is not the case.

Thus, I believe that when this is implemented, it should be documented somehow. Ember guides are not typed, so I don't know how/where. Maybe in the model blueprint?

Uhm, are blueprints even maintained? This is weird:
https://github.com/typed-ember/ember-cli-typescript-blueprints/blob/v3.0.0/blueprints/model/index.js#L70

Uhm, are blueprints even maintained?

Unfortunately, they are quite out of date at this point. We are exploring a better way to do the blueprints than having to re-implement each one for TypeScript (and then continually update as the default blueprints change). This will likely involve some form of automatically codemodding the default blueprints.

For the default async hasMany the type is:

DS.PromiseManyArray<T>

but for async: false it's:

DS.ManyArray<T>

I was thinking of creating AsyncHasMany and SyncHasMany but since async: true is default, maybe the async type should just be HasMany? Any thoughts on names?

馃憜馃徏 that PR has been released as @types/[email protected] and @types/[email protected].

Was this page helpful?
0 / 5 - 0 ratings