Reputation: 10697
I have the following function:
const ServerRequest = ({ token: ServerRequestOptions }) => ({
async() {}
})
and I'm not able to use the ServerRequest
as a directly type on my AuthService
because it is a function so, I have to do some "tricks" like:
const AuthService = (request: ReturnType<typeof ServerRequest>) => ({ })
For me this trick does not sound good because to use a simple reference to the ServerRequest I have to compose some "complex" types. I also tried to export it as a diff type:
type ServerRequestType = ReturnType<typeof ServerRequest>
But I see many naming conventions saying to "never" use pre/postfix like I
, Interface
, Type
, etc.
So, what should be the best path for this case?
Upvotes: 2
Views: 64
Reputation: 26637
You just need type which defines function signature.
interface ServerRequestOptions {/* ...*/}
// Proper function signature
type ServerRequestFunc = (options: {token: ServerRequestOptions}) => void;
// variable holding function
const serverRequest: ServerRequestFunc = ({ token: ServerRequestOptions }) => ({
async() {}
})
const authService = (request: ServerRequestFunc ) => ({ })
authService(serverRequest); // works fine.
Or even simpler,if we add name to our input parameter of serverRequest
:
const serverRequest = (options: { token: string }) => ({
async() {}
})
const authService = (request: typeof serverRequest) => ({ })
authService(serverRequest); // also works
Only, in 2nd example intellisense is not pretty (ex: hover over authService
function in IDE).
Upvotes: 1