microsoft / microsoft/TypeScript
The return value of function type is not checked when function may return any
Open
Nobody has claimed this yet.
Experimentation Needed
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 4.2.0-dev.20201124
Search Terms:
function, type check, any
Code
const f1: () => number = () => {
if (window) {
return {} as any;
}
return "a"; // Pass
};
// Expected behavior
const f2: () => number = () => {
return "a"; // Error: Type '() => string' is not assignable to type '() => number'
};
// Expected behavior
function f3(): number {
if (window) {
return {} as any;
}
return "a"; // Error: Type 'string' is not assignable to type 'number'.
};
Expected behavior:
f1 causes an error because it returns a string.
Actual behavior:
f1 does not cause an error.
Playground Link:
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 with the linked TypeScript Playground reproduction and compare the checks for f1, f2, and f3, focusing on return-type validation when one branch returns any. Trace the compiler's function return checking until the string return is diagnosed; done when f1 reports the expected type error without changing the valid cases.
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
- Mostly clear
- Newbie friendliness
- 35/100