microsoft / microsoft/TypeScript
Issue with 'this' Parameter and JSX Component Generics
@sandersn arbeitet bereits daran.
Seit 18.8.2023.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
"jsx component with this", "jsx component ThisParameterType"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about JSX
⏯ Playground Link
💻 Code
declare namespace JSX {
type LibraryManagedAttributes<C, P> = ThisParameterType<C>;
interface IntrinsicElements {
div: {};
}
}
function Test<T>(this: { value: T }) {
return <div>{ this.value }</div>
}
<Test<number> value={5} /> // value is unknown
🙁 Actual behavior
TypeScript is incorrectly inferring props types for JSX functional components with generic templates when props are derived from the 'this' parameter.
Even when explicitly typed as <number>, the Test component still exhibits the issue where the type of value remains unknown.
🙂 Expected behavior
It should infer the type from the passed data or at least accept that you explicitly type it with <Test<number> ...
Additional information about the issue
If you define a separate variable and assign the component with <Test<number>>, the value prop's type gets correctly inferred as number.
const TypedTest = Test<number>;
<TypedTest value={5} /> // value is number
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.