1

For this function:

function first(x: number, y: string) {
return {
    a: x,
    b: y
  };
}

function second(z: typeof first()) {
  return {
    c: 6,
    ...z
  }
}

typeof first() is, ofcourse, invalid,

I'd like to use the inferred output type of first in second's signature. Is it possible (I'm assuming no but hope to be surprised)

Thanks!

1 Answer 1

1

No, at this moment it is not possible to extract the return type of a function.

There is a thread on github where this is discussed though. You can see it here.

What you can do right now is to define a specific type to be used in as the return type of the first function and the type of the parameters received by the first function. It's also a lot more explicit and easier to understand.

interface ReturnType {
    a: number,
    b: string
}
function first(x: number, y: string): ReturnType {
    return {
        a: x,
        b: y
    };
}

function second(z: ReturnType) {
    return {
        c: 6,
        ...z
    }
}
Sign up to request clarification or add additional context in comments.

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.