#Mismatch in ED method `type` parameters and what actually gets passed

1 messages · Page 1 of 1 (latest)

wind jetty
#

We're on 3.28 and making the transition to TypeScript. Generally the experience has been well worth it, so a big thanks for the efforts of all involved.

In updating some of our adapters (which extend RESTAdapter, FWIW), the second parameter says it takes a Model, but modelName is a static field, and therefore not on the type from ember_data__model. typeof ember_date__model does include the static fields, but doesn't match the parent class. We've been using type.modelName for a while, and it's in the examples, so I'm guessing the type isn't quite right.

I did dig around and find the ShimModelClass, but I'm assuming I shouldn't depend on that.

Any recommended path forward would be appreciated.

silver walrus
#

@wind jetty the unofficial types don’t type adapters correctly. ShimModelClass is the shape you should be getting (we don’t ship those internal types… yet)

#

You may want to look at how we typed the community owner ActiveModelAdapter

wind jetty
#

The other thing I had considered was something like type: Parameters<RESTAdapter['query']>['1']

wind jetty
#

Which is hideous, but should stand up for a bit.

#

As would any 😬

#

Which is what the above currently evaluates to

#
type ObjMethod<C> = keyof {
  [P in keyof C as C[P] extends (...args: any) => any
    ? P
    : never]: any;
};
type MethodParam<
  Obj extends Record<string, any>,
  Method extends ObjMethod<Obj>,
  Position extends keyof Parameters<Obj[Method]>
> = Parameters<Obj[Method]>[Position];
type RESTAdapterQueryPrimaryModelClass = MethodParam<RESTAdapter, 'query', '1'>
#

Again, resulting in any 😂

silver walrus
#

You need something that extracts the schema from the type and assigns it to the static schema methods given to you by the (former ModelClass now ShimModelClass)

wind jetty
#

Think I'll stick with any until we're on a newer version of ED. Thanks for your help.

silver walrus
#

@wind jetty even newer versions have this issue

#

basically the problem is the community types (e.g. @types/ember-data) never got this type correct in the first place

#

we won't start shipping our own types from the internals until after 6.0 I'd think

#

but also cc @vestal totem this is one of those cleanups we need to do as we port the community types into a preview-types package

wind jetty
#

I could be blind, but none of those use modelName, which isn't on type Model from @ember_data/model

#

typeof Model includes the static fields and methods, but also a lot that are not in the shimModelClass

#

I could do a Pick<typeof Model, {shimModelClass stuff}> and get pretty close

#

But without that being a public type I'm setting up for a potential maintenance headache.

#

I'd be happy to PR an improved type there

silver walrus
#

PR away

#

happy to help there where I can

#
export interface ModelSchema {
  modelName: string;
  fields: Map<string, 'attribute' | 'belongsTo' | 'hasMany'>;
  attributes: Map<string, AttributeSchema>;
  relationshipsByName: Map<string, RelationshipSchema>;
  eachAttribute<T>(callback: (this: T, key: string, attribute: AttributeSchema) => void, binding?: T): void;
  eachRelationship<T>(callback: (this: T, key: string, relationship: RelationshipSchema) => void, binding?: T): void;
  eachTransformedAttribute<T>(
    callback: (this: T, key: string, relationship: RelationshipSchema) => void,
    binding?: T
  ): void;
}
#

^ this is roughly speaking what the type is

#
interface RelationshipSchema {
  name: string; // property key for this relationship
  kind: 'belongsTo' | 'hasMany';
  type: string; // related type

  options: {
    async: boolean;
    polymorphic?: boolean;
    as?: string;
    inverse: string | null; // property key on the related type (if any)
    [key: string]: unknown;
  };
}

interface AttributeSchema {
  name: string;

  kind?: 'attribute';

  options?: Record<string, unknown>;
  type?: string;
}
#

^ this is what the schema shapes should be

wind jetty
#

I'm not sure how to handle the v2/3 directories. Is the top-level index.d.ts v4? Do I need to make these same changes in the previous versions?

#

And I'm doing a sparse-checkout so I don't have the whole repo. I can't find glimmer types to run tests locally. Anyone know how to bring them in?