0

I have function like this

export const registerResource = <T, H, M, I extends object, R extends object>(serverConfig: IResourceServerConfig<T, H>, option: IRegisterResourceOption<T, H, M, I, R> = {}) => {
...
}

So I will call look like this

registerResource(foo,bar)

But I would like to definition only 2 type T & H like this

registerResource<any, number>(foo,bar)

But it's not work, did I'm doing stupid and how can I resolve that.

Thanks alot

1 Answer 1

1

If you want to specify T and H at the call site and let the remaining type arguments be inferred, that feature is not currently supported by TypeScript but hopefully it will be soon; see the open suggestion. The current workaround is to change your function into a function with some type parameters that returns a function with the remaining type parameters:

export const registerResource = <T, H>(serverConfig: IResourceServerConfig<T, H>) =>
  <M, I extends object, R extends object>(option: IRegisterResourceOption<T, H, M, I, R> = {}) => {
  // ...
};
registerResource<any, number>(foo)(bar);

(I assume your example was based on real code that is actually using M, I, and R for something, otherwise you could just replace them with {}, object, and object and not have this problem.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I think if I change my func into 2 curry func(func => func), It's look weird in my case, 2nd way is also work but it will loose some properties: ` const a = registerResource<IPractitioner, IDataServerResponse<IPractitioner>, any, object, object>(serverConfig, { addResourceProps: (resource) => ({newOne: 1}) }) ` In that case it's will not receive the newOne is also the properties to return a.resource.newOne (not work). Sample code at here: gist.github.com/tomzaku/f46e5341ff82527ff530fc25f8f3e5e8
I don't have another solution to offer at this time than the "weird" currying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.