8000 Hook typing and find method parsing issue · Issue #2147 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content

Hook typing and find method parsing issue #2147

@soullivaneuh

Description

@soullivaneuh

I use the Hook type when declaring my hook:

type PushToken = {
  token: string;
  applicationSlug: string;
  createdAt?: Date;
}

const handleCreate: Hook<PushToken> = ({ data, service, result }) => {
  if (!data) {
    return;
  }
  const { token, applicationSlug } = data;

  return service
    .find({ query: { token }, paginate: false })
    .then((tokens) => tokens.length)
  ;
}

Works fine, except for the last then callback.

The tokens.length throw the following typescript error:

Property 'length' does not exist on type 'PushToken | PushToken[] | Paginated<PushToken>'.
  Property 'length' does not exist on type 'PushToken'.ts(2339)

This is caused by the following signature:

find (params?: Params): Promise<T | T[] | Paginated<T>>;

I may have only one instance but also an array or a Paginated instance.

So I tried to force the typing like this:

.then((tokens: PushToken[]) => tokens.length)

But then give me now an error on the function typing itself:

Argument of type '(tokens: PushToken[]) => number' is not assignable to parameter of type '(value: PushToken | PushToken[] | Paginated<PushToken>) => number | PromiseLike<number>'.
  Types of parameters 'tokens' and 'value' are incompatible.
    Type 'PushToken | PushToken[] | Paginated<PushToken>' is not assignable to type 'PushToken[]'.
      Type 'PushToken' is missing the following properties from type 'PushToken[]': length, pop, push, concat, and 26 more.ts(2345)

The only way I have to get rid of this error is to set the any type to my tokens params, but I loose all the interest of typescript then.

Not sure if it's a bug, but I can't find any reliable example for that on the documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0