8000 feathers service declaration · Issue #2005 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content

feathers service declaration #2005

@bwgjoseph

Description

@bwgjoseph

Hi,

By default, when generating a new service (Post) using feathers-cli will result in this interface declaration.

// Add this service to the service type index
declare module '../../declarations' {
  interface ServiceTypes {
    'post': Post & ServiceAddons<any>;
  }
}

Doing so, will not give us the overload type that is defined.. so we lose the overload methods declaration..

Made a small tweak to include interface for it, to illustrate it better

interface PostInterface {
  title: string;
  message: string;
}

// Add this service to the service type index
declare module '../../declarations' {
  interface ServiceTypes {
    'post': Post & ServiceAddons<PostInterface>;
  }
}

image

Notice that, it does not provide the overload methods by default as shown in the image above.

Now, if we were to declare as such..

// Add this service to the service type index
declare module '../../declarations' {
  interface ServiceTypes {
    'post': Post & ServiceOverloads<PostInterface> & ServiceAddons<PostInterface>;
  }
}

image

IDE will be able to pick up the correct overload types and display as such (shown in the image above)

So I thought that the default, should as such instead of Post & ServiceAddons<PostInterface>

To add-on, I'm not sure if there's actually any difference between declaring

// this
declare module '../../declarations' {
  interface ServiceTypes {
    'post': Post & ServiceOverloads<PostInterface> & ServiceAddons<PostInterface>;
  }
}

// and this
declare module '../../declarations' {
  interface ServiceTypes {
    'post': Post & Service<PostInterface>;
  }
}

Since that Service is defined as such type Service<T> = ServiceOverloads<T> & ServiceAddons<T> & ServiceMethods<T>;


So my proposal, is to switch from using

'post': Post & ServiceAddons<PostInterface>;

to

'post': Post & Service<PostInterface>;
'post': Service<PostInterface> & Post;

update: and because of the way it's defined, the order matters (due to overloading?, or intersecting types?), if define Post & Service<PostInterface>, it will not "see" the ServiceOverloads<T> first. Which will cause this to happen.

note: the interface is different but the idea remains.

image

image

I would have to cast manually - .create(data) as PostInterface

But if the definition is switched to 'post': Post & Service<PostInterface>; It knows it is a single item, and therefore, used the correct overload method and return, without the need to cast.

image

image


The line to change is probably this

https://github.com/feathersjs/generator-feathers/blob/5212ac5ac26d72242abd8aa1072c376821bc8756/generators/service/templates/ts/service.ts#L11

If this is alright, I can create a PR for that. Let me know what you think of it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0