-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript reports an error when trying to use a variable of type string as JSX element identifier #28806
Comments
Duplicate of #28768? |
@ajafff Indeed seems like it. Could you mark it as a dupe? |
No, I doubt this is a dupe - you're not dealing with a union of tags here. |
@MadaraUchiha To clarify the issue, do you expect declare var Element: string;
const tag = <Element onClick={() => 42} />; to work? |
Yes. My specific usecase is a I'd expect either |
React's intrinsic elements don't have an indexer - I don't see how an arbitrary |
@weswigham The code that I wrote definitely compiles and passes typechecking before 3.2. I honestly don't know how or what it infers (I'm guessing It's worth noting that it passes before 3.2 with or without |
Goes to check old code It should have at least been a |
As you said yourself - it allowed passing arbitrary garbage props, so was patently unsafe. You can explicitly opt-in to an checked tag by casting the tag in question let El: any = "div"; // El is unchecked and will allow any props to be passed to it
const x = <El onClick={() => 42} />; |
@weswigham This is an acceptable workaround for me for now. However, taking it one step further, would it be possible to make it actually safe? Is it possible to making it safe after all? I can already foresee problems with the different kind of SyntheticEvents that React has. |
@MadaraUchiha A combination of limiting your input to |
@weswigham so if i understand correctly, it does accept a string inside We are having the same error with:
How is that code unsafe? we just use "common" properties of the union type. ie common properties like |
Yeah, that's the part where I said we needed to do better - the props of an |
@weswigham That makes it sounds like a dupe of #7294. Is that correct? |
Yes - how |
TypeScript Version: 3.2.1, 3.3.0-dev.20181201
Search Terms: TypeScript JSX string type, TypeScript JSX string element
Code
Expected behavior:
Before (and not including) 3.2.0, this code emitted no errors.
Actual behavior:
TypeScript emits error:
Playground Link: N/A - The playground doesn't support JSX.
Related Issues:
Not on GitHub, but the release announcement specifically mentioned this as a possible regression, and asked to report it.
It seems like it infers a type of either
{}
orReact.IntrinsicAttributes
now, instead of what used to beany
.I'm not sure what's the correct course of action here, but upgrading to 3.2 made a previously compiling code to not compile due to this, so I'm currently forced to revert to 3.1.6.
Ideas?
cc @aizikdahan @Linkgoron @Moshiko1989
The text was updated successfully, but these errors were encountered: