microsoft / microsoft/TypeScript
JS checking makes JSX props optional when inferred through JSX.LibraryManagedAttributes
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Bug Report
Reported as part of #45140, moving to its own issue.
🔎 Search Terms
js jsx props optional
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
// @Filename: index.jsx
import React from "react";
import PropTypes from "prop-types";
function MyComponent({ prop1, prop2, prop3 }) {
return (
<div>
<span>{prop1}</span>
<span>{prop2}</span>
</div>
);
}
MyComponent.propTypes = {
prop1: PropTypes.string.isRequired,
prop2: PropTypes.bool,
prop3: PropTypes.shape({
p1: PropTypes.string
})
};
// All props show as optional in quick info / completions, and there's no error for missing prop1.
// The type of the JSX props is inferred from `MyComponent.propTypes` via JSX.LibraryManagedAttributes,
// but the optionality appears wrong. Switching the language to TypeScript makes this behave as expected.
(<MyComponent prop2 />)
🙁 Actual behavior
All props show as optional in quick info / completions, and there's no error for missing prop1. The type of the JSX props is inferred from MyComponent.propTypes via JSX.LibraryManagedAttributes, but the optionality appears wrong. Switching the language to TypeScript makes this behave as expected.
🙂 Expected behavior
The prop1 JSX prop shows as required and an error is shown on the JSX element since it’s missing.
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 el ejemplo index.jsx, centrándote en JSX.LibraryManagedAttributes y la asignación de propTypes. Compara la comprobación de JavaScript con el mismo código en TypeScript para rastrear por qué se pierde la obligatoriedad. Se considera terminado cuando prop1 se muestra como obligatorio y produce un error por una prop ausente.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, react, typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100