T | (() => T)
See original GitHub issueTypeScript 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:
- Created 3 years ago
- Reactions:43
- Comments:25 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Found that this reveals the issue:
But this does not (and compiles file):
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)
Ran into this while trying to use the
SetStateAction<T>
type inreact
:If using
typeof foobar === 'function'
isn’t the correct way to check whether we have a value, or a callable initializer function, what is?