1

I see one example to use Mapped types for type maps to create a type from object and I would like to use that in my case as following so I can replace the Function type to fix the eslint error Don't use 'Function' as a type. The 'Function' type accepts any function-like value. It provides no type safety when calling the function, which can be a common source of bugs.

enum ApiEnum {
  CREATE_NAME = 'CREATE_NAME',
  CREATE_COMPANY = 'CREATE_COMPANY',
  UPDATE_NAME = 'UPDATE_NAME',
}

type CreateName = (variables: number) => Promise<{data: number}>;
type CreateCompany = (variables: string) => Promise<{data: string}>;
type UpdateName = (variables: Record<string, number>) => Promise<{data: Record<string, number>}>;

function createName(variables: number){
    return {data: variables}
}

function createCompany(variables: string){
    return {data: variables}
}

function updateName(variables: Record<string, number>){
    return {data: variables}
}

// error from eslint

// Don't use `Function` as a type. The `Function` type accepts any function-like value.

const api: Map<ApiEnum, Function> = new Map([
  [ApiEnum.CREATE_NAME, createName as Function],
  [ApiEnum.CREATE_COMPANY, createCompany as Function],
  [ApiEnum.UPDATE_NAME, updateName as Function],
]);


jacobcan118
  • 7,797
  • 12
  • 50
  • 95
  • 1
    Please consider modifying the code in this question so as to constitute a [mcve] which, when dropped into a standalone IDE like [The TypeScript Playground (link here!)](https://tsplay.dev/WPjQkN), clearly demonstrates the issue you are facing (with no typos, unrelated errors, or undeclared types or values). This will allow those who want to help you to immediately get to work solving the problem without first needing to re-create it. And it will make it so that any answer you get is testable against a well-defined use case. – jcalz Sep 13 '21 at 19:49
  • 1
    Also, note that you should describe what problem you are having, specifically. All you've said here is that it "does not work" and that you have had no luck, but it's not clear what kind of problem you're having. – jcalz Sep 13 '21 at 19:51
  • thank you. i update my question – jacobcan118 Sep 13 '21 at 20:37
  • When I try to look at your code, [see here](https://tsplay.dev/wRJQxw), I see many many errors, most of which are probably unrelated to what you're asking, right? Please remove those errors from the example code so that your issue is easy to see. – jcalz Sep 13 '21 at 20:44
  • those error and type and function from my application – jacobcan118 Sep 13 '21 at 20:52
  • Yes, and you should probably remove those from your question, unless you are asking about them specifically. Without a [mcve] I'm not likely to try to engage further here. It is possible you are asking [this question](https://stackoverflow.com/q/54907009/2887218), or maybe something else, but without code I can test I can't say for sure. – jcalz Sep 13 '21 at 21:04
  • fair enough, I update my question again – jacobcan118 Sep 13 '21 at 23:24
  • So is this specifically an eslint issue? You could maybe do [this](https://tsplay.dev/mqvldW) to avoid that particular error; does that meet your needs or is there something else you're trying to do? – jcalz Sep 13 '21 at 23:31
  • Btw, you can use `(...args:any[])=>any` instead of `Function` – captain-yossarian from Ukraine Sep 14 '21 at 06:20

0 Answers0