-
Notifications
You must be signed in to change notification settings - Fork 12.8k
JSX elements should not rely on only global JSX.Element type #4195
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
Comments
I don't see any value in a JSX expression meaning one thing in one place and one thing in another. It's similar to |
Ok I think I can agree on that. |
Okay, so I'm relatively new to TypeScript and just trying to figure stuff out. Does this kind-of incidentally imply that React or another JSX-utilizing library can't be loaded through the module system, and instead must be global? I say this because that's what seems to be the case. If I define the JSX interface manually somewhere else, I can't tie it back to the local definition of React, for example. If this is the intended behavior, then it is somewhat inconsistent, because |
JSX is just sugar for plain JavaScript calls. Nothing about it is global, and its types shouldn't be either. Given the docs, I was under the impression that "JSX type" would be determined by a local lookup of what was set by the
Nowadays there's plenty of compatible JSX libraries, so there's clearly as case for being able to write components that're compatible with different libraries. TypeScript is great for ensuring compatibly with multiple libraries. However, TypeScript makes this difficult, because the JSX type is global. The following code is valid, but fails type checks. import * as assert from 'assert';
import { h } from 'preact';
import * as render from 'preact-render-to-string';
import * as React from 'react';
import * as ReactDomServer from 'react-dom/server';
type JsxHandler<Output> = (node: any, props?: any, ...children: any[]) => Output;
const props = { hello: 'Hello World' };
function makeComponent<T>(h: JsxHandler<T>) {
return (props: any) => <div>{props.hello}</div>;
}
const PreactComponent = makeComponent(h);
const ReactComponent = makeComponent(React.createElement);
assert.equal(
render(PreactComponent(props)),
ReactDomServer.renderToStaticMarkup(ReactComponent(props))
); |
I find this to be very limiting:
It doesn't complain if I remove the export
keyword
. Why does it need to rely on a global type? Can't it not be local? I want to develop a JSX framework and I want to create a local type ofJSX.Element
etc. and then compile the definition file. I did began with declaringJSX.Element
in ad.ts
file, but there was some limitation with that approach. Also all my modules is of external type.The text was updated successfully, but these errors were encountered: