user3312156
user3312156

Reputation: 153

Is there a way for typescript to infer arguments object or have a type defined for arguments object?

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>) } }

Upvotes: 0

Views: 42

Answers (2)

resolritter
resolritter

Reputation: 331

Try using the built-in Parameters

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

const fn = (...args: Parameters<success>) => {}

Upvotes: 2

Michel Vorwieger
Michel Vorwieger

Reputation: 722

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.

Upvotes: 0

Related Questions