Typescript function parameter into returnType

Here's my function:
export const useSomething = <Data extends object>(
func: (values?: Data) => Promise<any>,
name?: string
)
export const useSomething = <Data extends object>(
func: (values?: Data) => Promise<any>,
name?: string
)
It returns as below
return {} as returnType<Data,Name>
return {} as returnType<Data,Name>
I want to use the function in the below syntax:
useSomething<SomeInterface>(TestFunction, 'test')
useSomething<SomeInterface>(TestFunction, 'test')
and not
useSomething<SomeInterface,'test'>(TestFunction, 'test')
useSomething<SomeInterface,'test'>(TestFunction, 'test')
🤨 How can I create the type Name - from the value of the parameter name- and use it in the returnType thing so I avoid typing 'test' twice in the call?
1 Reply
Sybatron
Sybatron•2y ago
export const useSomething = <Data extends object>(
func: (values?: Data) => Promise<unknown>,
name?: string
) => {
type returnType = { data1: Data; data2: string };
return {
data1:
/*data that can satisfy Data*/
as Data,
data2: "test",
} satisfies returnType;
};
export const useSomething = <Data extends object>(
func: (values?: Data) => Promise<unknown>,
name?: string
) => {
type returnType = { data1: Data; data2: string };
return {
data1:
/*data that can satisfy Data*/
as Data,
data2: "test",
} satisfies returnType;
};
ReturnType takes only 1 argument you cant add 2 types there so just your own new type that you want to satisfy
Want results from more Discord servers?
Add your server