0

I have a function with a signature

const updateMatrix = (m: number) => (i: number, j: number, value: number): void => {

However, I need to re-use the second function's signature in other parts of my code, so I set it to a type.

export type SetMatrixValue = (i: number, j: number, value: number) => void

How can I set the type of my updateMatrix function to match that of the SetMatrix Value type?

1 Answer 1

2

You could extract the type of the second function using ReturnType utility (it is actually return type of updateMatrix):

const updateMatrix = (m: number) => (i: number, j: number, value: number): void => {}

type SetMatrixValue = ReturnType<typeof updateMatrix>; // (i: number, j: number, value: number) => void

Playground


Answering original question:

type SetMatrixValue = (i: number, j: number, value: number) => void

const updateMatrix = (m: number): SetMatrixValue => (i, j, value) => { };
Sign up to request clarification or add additional context in comments.

1 Comment

I want it the other way around, I want to be able to pass the type TO the function to serve as both its arguments and return type, so I can re-use it in other functions in my code.

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.