microsoft / microsoft/TypeScript
Contextual typing fails to throw error with function return type on left side of equals sign
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el TypeScript Playground enlazado y reproduce el ejemplo usando el tipo de retorno de la función en el lado izquierdo del signo igual. Compara el comportamiento con el tipo de retorno en el lado derecho y con el primer caso de switch eliminado. Se considera terminado cuando se informe de la propiedad ausente requiredNumber en su forma original.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 48/100