Ember Data @attr options and Typescript

Hi all, have a quick question about TypeScript if anyone had the bandwidth!

I’m at the start of the journey with ember-cli-typescript and refactoring some Ember Data Models.

I have an enum @attr and I am passing some custom options down like so:

import Model, { attr } from '@ember-data/model';
import { someArrayOfStrings } from 'my-app/enumOptions';

export default class BookingModel extends Model {
  @attr('enum', {
    enumOptions: salesChannels,
    defaultValue: '',
  })
  declare salesChannel?: string;
}

declare module 'ember-data/types/registries/model' {
  export default interface ModelRegistry {
    booking: BookingModel;
  }
}

My Transform isn’t doing much:

import Transform from '@ember-data/serializer/transform';

declare module 'ember-data/types/registries/transform' {
  export default interface TransformRegistry {
    enum: EnumTransform;
  }
}

export default class EnumTransform extends Transform {
  deserialize(serialized: string): string {
    return serialized;
  }

  serialize(deserialized: string): string {
    return deserialized;
  }
}

TypeScript is complaining about the @attr and the error is as follows;

No overload matches this call.
  Overload 1 of 3, '(type: "enum", options?: AttrOptions<string> | undefined): ComputedProperty<string, string>', gave the following error.
    Argument of type '{ enumOptions: salesChannels[]; defaultValue: string; }' is not assignable to parameter of type 'AttrOptions<string>'.
      Object literal may only specify known properties, and 'enumOptions' does not exist in type 'AttrOptions<string>'.
  Overload 2 of 3, '(target: any, propertyKey: string): void', gave the following error.
    Argument of type '{ enumOptions: salesChannels[]; defaultValue: string; }' is not assignable to parameter of type 'string'. 
ts(2769)

I take it I need to update/extend AttrOptions are but I am unsure how to do it - any ideas?

Hey @roomman I was running into a similar situation. Added a PR that will hopefully help with this [ember-data] additional typing for transforms by toh995 · Pull Request #62874 · DefinitelyTyped/DefinitelyTyped · GitHub

The PR has been merged, and after upgrading my @types/ember-data, the type error has gone away for me.

1 Like