microsoft / microsoft/TypeScript

Issue with 'this' Parameter and JSX Component Generics

Aperta
#55,431 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@sandersn ci sta già lavorando.

Dal 18/8/2023.

Bug Domain: This-Typing Rescheduled
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 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

https://www.typescriptlang.org/play?jsx=1&ts=5.3.0-dev.20230818#code/CYUwxgNghgTiAEA7KBbEBnADlMCBSAygBrwDeAUPPAC4CemCAMgJYBGMstAslMgOYhgAQWrUYbAK7UMAHgDCAGngAFAHzwAvPAAqAC2bplsVCGkxt9EPNUBucpXjNEZgGY4EASWfjE6ZmABRCBA0Z3QyBypgZgA3AC4yAF87KkTyNPIXCUQwamYAe0QdDGoZbVUACmp9dATSeBioCAkQBO14RIBKCKo4agkYIplomNV66oMAOkbmhESZAHoR1XT7MpKZRAkUVhAYdRmWjVIAVkT4BZXyMEL0ah1LYG0SzWK7ze3d-btyMsfnu4NJpHU7nS7kIA

💻 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

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.