microsoft / microsoft/TypeScript

Contextual typing fails to throw error with function return type on left side of equals sign

Open
#38,879 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Contextual Types
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: I got the same behavior on 3.7.4 and 4.0.0-dev.20200601

Search Terms:
left side
right side
contextual typing
type inference

Code

interface ReportState {
  reportDefinition: {
    [key: number]: ReportDetails;
  };
}

interface ReportDetails {
  requiredNumber: number;
  columnWidths?: number[];
}

export const Report: (state: ReportState, action: any) => ReportState = (
  state,
  action
) => { // See note below about this line
  switch (action.type) {
    case "TYPE0": {
      const { reportDef, sequenceId } = action.payload;
      return {
        ...state,
        reportDefinition: {
          ...state.reportDefinition,
          [sequenceId]: {
            ...reportDef,
            selectedElements: ["0x0"],
          },
        },
      };
    }

    case "TYPE1": {
      return {
        ...state,
        reportDefinition: {
          ...state.reportDefinition,
          [action.payload.sequenceId]: {
            // requiredNumber is missing here. This should be an error.
            columnWidths: action.payload.columnWidths,
          },
        },
      };
    }

    default:
      return state;
  }
};

Expected behavior:
There should be an error, because requiredNumber is missing from the second return statement.

Actual behavior:
Compilation succeeds without error.

Curiosity 1:
If you add the return type on the right side of the =, i.e. by changing
) => {
to
): ReportState => {
, it correctly finds the error. This should not be necessary, because the return type is already specified on the left side of the =.

Curiosity 2:
If you remove the first case block, it correctly finds the error (which is in the second case block).

Playground Link: Click here

Related Issues:
No

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 and reproduce the example using the function return type on the left side of the equals sign. Compare behavior with the return type on the right side and with the first switch case removed. Done means the missing requiredNumber property is reported in the original form.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.