Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript service typing doesn't allow addition of custom services #14035

Closed
Bassel17 opened this issue Aug 9, 2022 · 5 comments · Fixed by #14061
Closed

Typescript service typing doesn't allow addition of custom services #14035

Bassel17 opened this issue Aug 9, 2022 · 5 comments · Fixed by #14061
Assignees
Labels
issue: bug Issue reporting a bug source: typescript Source is related to TypeScript (typings, tooling, ...)

Comments

@Bassel17
Copy link
Member

Bassel17 commented Aug 9, 2022

Bug report

Required System information

  • Strapi version: 4.3.2

Describe the bug

Typescript service typing doesn't allow addition of custom services, when adding a new service such as follows:

import { factories } from '@strapi/strapi'; 

export default factories.createCoreService('api::something.something', ({strapi}) =>  ({
  // Method 1: Creating an entirely custom service
  async exampleService(...args) {
    let response = { okay: true }

    if (response.okay === false) {
      return { response, error: true }
    }

    return response
  },
}));

It throws a typing error as follows:

image

Expected behavior

Should work and not throw an error

@Bassel17 Bassel17 added issue: bug Issue reporting a bug source: typescript Source is related to TypeScript (typings, tooling, ...) labels Aug 9, 2022
@Bassel17 Bassel17 self-assigned this Aug 9, 2022
@emahuni
Copy link
Contributor

emahuni commented Sep 14, 2022

apparently there must have been a regression, this is no longer working.

image

I was on Strapi 4.3.6, and now on 4.3.8 and on both versions it didn't work properly.

This removes that error, but then introduces another one

image

image

If I change the GenericService type to this it works:
image

from this:
image

notice any instead of T as the return type

@emahuni
Copy link
Contributor

emahuni commented Sep 14, 2022

even the code in this issue doesn't work

@emahuni
Copy link
Contributor

emahuni commented Sep 14, 2022

putting any as the return type completely fixed the issue such that this works without a problem:

export type GenericService = Partial<Service> & {
  [method: string | number | symbol]: <T = any>(...args: any) => any;
};


export default factories.createCoreService('api::something.something', ({ strapi })  => {
  return {
    // service functions or properties here
    async someFunction (a: number): Promise<number[]> {
      return [1, 2, 3];
    },
    
    async someFunction2<T> (a: number): Promise<T[]> {
      return [1 as T];
    },
  };
});

notice the generic example at the end, it still works

@saintego
Copy link

saintego commented Nov 1, 2022

@emahuni how did you change GenericService without editing factories in lib? Without it I have error on createCoreService

@emahuni
Copy link
Contributor

emahuni commented Nov 4, 2022

I created my own typings file then added that type

export type GenericService = Partial<Service> & {
  [method: string | number | symbol]: <T = any>(...args: any) => any;
};

If you put this directly in the file you're using this it'll still work, putting it into a separate typings file makes it cleaner and reusable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug source: typescript Source is related to TypeScript (typings, tooling, ...)
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants