question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript Version: 3.9.0 (Nightly)

Search Terms: T | (() => T), T & Function

Code

type Initializer<T> = T | (() => T)
// type Initializer<T> = T extends any ? (T | (() => T)) : never

function correct<T>(arg: Initializer<T>) {
    return typeof arg === 'function' ? arg() : arg // error
}

Line 2 provides a workaround for this. More info on stackoverflow.

Expected behavior: no errors

Actual behavior: This expression is not callable. Not all constituents of type '(() => T) | (T & Function)' are callable. Type 'T & Function' has no call signatures.

Playground Link: here.

Related Issues: none

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:43
  • Comments:25 (4 by maintainers)

github_iconTop GitHub Comments

69reactions
kentcdoddscommented, Jun 8, 2021

Found that this reveals the issue:

function a<T>(b: T | (() => T)) {
  return typeof b === 'function' ? b() : b
}

But this does not (and compiles file):

function a<T>(b: T | (() => T)) {
  return b instanceof Function ? b() : b
}

Kind of odd, because typeof checks seem to work normally. But this is a reasonable workaround for anyone who bumps into it. HT to @pbteja1998 😃

NOTE: this workaround can have problems in some situations: https://github.com/microsoft/TypeScript/issues/37663#issuecomment-856866935 Check a safer (though more verbose) workaround: https://github.com/microsoft/TypeScript/issues/37663#issuecomment-856961045)

23reactions
Svishcommented, Jan 6, 2021

Ran into this while trying to use the SetStateAction<T> type in react:

type SetStateAction<S> = S | ((prevState: S) => S);

If using typeof foobar === 'function' isn’t the correct way to check whether we have a value, or a callable initializer function, what is?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Notation in linear algebra, what are $N(T)$ and $R(T)
In this problem, T:P4(R)→R4 is a linear transformation, and there's a formula given to define it. No problem there.
Read more >
Arrow function expressions - JavaScript - MDN Web Docs
An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate ...
Read more >
numpy.ndarray.T — NumPy v1.24 Manual
numpy.ndarray.T#. attribute. ndarray.T#. View of the transposed array. Same as self.transpose() . See also. transpose. Examples. >>> a = np.array([[1, 2], ...
Read more >
Optional (Java Platform SE 8 ) - Oracle Help Center
Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional . T · orElse(T other). Return the value if...
Read more >
Trace (linear algebra) - Wikipedia
In linear algebra, the trace of a square matrix A, denoted tr(A), is defined to be the sum of elements on the main...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found