microsoft / microsoft/TypeScript

The return value of function type is not checked when function may return any

Open
#41,696 2 comments 0 reactions 0 assignees View on GitHub

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:

https://www.typescriptlang.org/play?ts=4.2.0-dev.20201124#code/MYewdgzgLgBAZgRgFwwBQEoYF4B8MwCuAtgEYCmATtmprjAN4BQMMAlnGgO6tgAmInTExYsKZKAQpgGAXxgBDCArABPANzMYMzWIlSYAInkG1MAPRmYABUURGMjYwswAogA8ADmWBQyvGOQAFvIAbqwgFIygkLBwAEwoGNh4hKSU1El0wjC6ktJGJuaWLhQUESgAKipeMADkmXjQFDwA5rVsSmAgsLasLWDyJAA2ZDBQIGPVo-W0KcTkFLX2js7uXj5+AWTBYRGMcARgPuHScADMGCipCwya7Fw8-IK3IjniebIKSvKqGizaone+gKplWpXKMCqNVqTVa7VYnW6XwgfQGw1G40m0OulFqADplowgA

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.