0

I have a type like the following:

type success = (data: any, value: string, settings: any) => void

OR an interface like the following

// interface success { (data: any, value: string, settings: any): void }

I have an object like this { onSuccess: (<need to have 'success' type here for arguments>) => { callbackFunction(<call using the same arguments passed to OnSuccess function>) } }

  • Is there a way to have arguments object passed to onSuccess function be type inferred to success type? If so how do I define an interface or a type signature similar to success type above that can be used to be type inferred?
3
  • Avoid arguments keyword as much as possible Commented Oct 30, 2020 at 19:48
  • what is the signature of the callbackFunction ? Commented Oct 30, 2020 at 19:51
  • @MichelVorwieger The callbackFuntion expects the following callbackFunction(data: any, value: string, settings: any). If you notice, the arguments signature is the same as the success type above Commented Oct 30, 2020 at 20:00

2 Answers 2

2

Try using the built-in Parameters

type success = (data: any, value: string, settings: any) => void

const fn = (...args: Parameters<success>) => {}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This was what I was looking for. Did not know there was Parameters available
0

if the order of the object arguments are always the same you might be able todo something with Object.values and the spread operator

Object.values: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/values

> const fn = (a, b, c) => ({a,b,c})
undefined
> fn({a: 5, b: 3, c:2})
{ a: { a: 5, b: 3, c: 2 }, b: undefined, c: undefined }
> fn(...Object.values({a: 5, b: 3, c: 2}))
{ a: 5, b: 3, c: 2 }

where Object.values({a: 5, b: 3, c: 2}) is equal to [5, 3, 2]

Although I would not recommend this and would just add some code to map them directly to prevent bugs later on.

Comments

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.