microsoft / microsoft/TypeScript
T | (() => T)
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked TypeScript Playground repro and compare the direct Initializer definition with the distributive conditional workaround. Trace the type narrowing and callability behavior involved in the typeof check. Done means the direct definition compiles without the reported error while preserving the expected type behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100