Microsoft/TypeScript
在 GitHub 查看Component not always a valid JSX component depending on a generic type
Open
#54,702 建立於 2023年6月19日
Domain: JSX/TSXHelp WantedPossible Improvement
描述
Bug Report
🔎 Search Terms
jsx react generic valid JSX
🕗 Version & Regression Information
- This changed between versions 5.0.4 and 5.1.3
⏯ Playground Link
💻 Code
import * as React from "react";
const A = <T extends unknown>({ a }: { a: T }) => (typeof a === "string" ? null : new Error("a must be a string")) as T extends string ? null : Error;
const jsxWithError = <A<string> a="a" />;
const B = A<string>;
const jsxWithoutError = <B a="a" />;
🙁 Actual behavior
Error on A in jsxWithError:
'A' cannot be used as a JSX component.
Its type '<T extends unknown>({ a }: { a: T; }) => T extends string ? null : Error' is not a valid JSX element type.
Type '<T extends unknown>({ a }: { a: T; }) => T extends string ? null : Error' is not assignable to type '(props: any, deprecatedLegacyContext?: any) => ReactNode'.
Type 'Error' is not assignable to type 'ReactNode'.(2786)
🙂 Expected behavior
No error with the generic type provided (as is the case for jsxWithoutError), since we are sure we are using a valid JSX component.