0

i have a project with nextjs and typescript.i have a function with name of submit func. this function accept three argument.type with string type base end point with string type and my problem is whit third argument and i can't handle type for this argument.i want this argument accepted a object with dynamic number of keys and values and also that values can have string or number value. this is my submit func

 interface values {
    [key: string]: object;
}
 const submitFunc = (type: string, baseEndPoint: string, values: values) => {
 //some code
 }

for more explanation i want for example call my function as follow

submitFunc('login', '/auth/mobile', {
            'mobile': 0912855838,
            'password': 'serer434312'
        });

or

submitFunc('name', '/auth/name', {
            'name':'Max'
        });

and Etc

I would be very grateful if anyone could help me

Ali Ehyaie
  • 1,116
  • 3
  • 13
  • 27

2 Answers2

2
interface values {
    [key: string]: string | number;
}

This will define an object with key as string and values as either string or number type

N.K.
  • 464
  • 2
  • 4
1

Use Record<string, string | number>:

const submitFunc = (
  type: string,
  baseEndPoint: string,
  values: Record<string, string | number>
) => {
  // your code here
};

Also, in your particular case, you may also join multiple types:

type Values = { mobile: number; password: string } | { name: string };

const submitFunc = (type: string, baseEndPoint: string, values: Values) => {
  // your code here
};

Or if you want to have all the combinations of those three keys, you can define optional properties:

type Values = { mobile?: number; password?: string; name?: string };

const submitFunc = (type: string, baseEndPoint: string, values: Values) => {
  // your code here
};
brc-dd
  • 10,788
  • 3
  • 47
  • 67