Contextual typing fails to throw error with function return type on left side of equals sign
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 48/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- À l'abandon
- Stack technique
- typescript
- Domaine
- compilers
Piste de recherche
Commence par le TypeScript Playground lié et reproduis l’exemple en utilisant le type de retour de la fonction à gauche du signe égal. Compare le comportement avec le type de retour à droite et avec le premier case du switch supprimé. La tâche est terminée lorsque la propriété requiredNumber manquante est signalée sous sa forme originale.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
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
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de microsoft/TypeScript
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
microsoft/TypeScript#64322 · 2 commentaires · 1 réaction · 2 personnes assignées ·
-
Possible Improvement
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
microsoft/TypeScript#64278 · 1 commentaire · 1 réaction ·
-
Docs
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
microsoft/TypeScript#64118 · 1 commentaire ·
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
microsoft/TypeScript#64094 ·
-
Docs
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
microsoft/TypeScript#63959 · 5 commentaires ·
Toutes les issues de microsoft/TypeScript
Issues similaires
-
optimization optimization:agents-md-curator
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
blinklabs-io/bursa#904 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 commentaires ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 90/100