1

Consider the following code in TS playground here

In line 14, is it possible to throw error for "tree" variable, i.e. it does not exist in the returned object for it to be destructured ?

const foo = () => ({
  bar: {
    hi: 5
  },
  six: {
    max: 10
  }
})

function test(fn: () => Record<string, Object>) {
  return fn()
}

const {six, tree } = test(foo) // <- line 14

console.log(tree)

1 Answer 1

2

Yes, with the use of generics you can:

function test<R extends Record<string, Object>>(fn: () => R): R {
    return fn()
}

Playground Link

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.