microsoft / microsoft/TypeScript

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

Aperta
#38,879 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Contextual Types
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e riproduci l’esempio usando il tipo di ritorno della funzione sul lato sinistro del segno di uguale. Confronta il comportamento con il tipo di ritorno sul lato destro e con il primo case dello switch rimosso. Il lavoro è completato quando la proprietà mancante requiredNumber viene segnalata nella forma originale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.