Description
TypeScript Version: 2.0.3 / nightly (2.1.0-dev.201xxxxx)
I think we should add another type to the "jsx" option in tsconfig.json named "local". This would expect a "createElement" function to be in scope when rendering .tsx files.
I think this mode would allow VueJS + TSX to work better. Currently, the "preserve" option needs to be set and then another transformer (there is a babel transformer) needs to be used on the output file. I think the integration would be a lot simpler with that change and wouldn't require a second transpiler.
I also think this change would make it easier for future libraries since they won't have to register globals, but instead simply supply a function that matches the react/vue createElement syntax.
Here is how I expect it to work:
The following would compile properly
TSX file
import { style } from 'typestyle';
import { HelloComponent } from './components'
const appStyles = style({
fontFamily: 'Avenir, Helvetica, Arial, sans-serif',
'-webkitFontSmoothing': 'antialiased',
'-mozOsxFontSmoothing': 'grayscale',
textAlign: 'center',
color: '#2c3e50',
marginTop: 60,
});
export class AppComponent {
name = 'app';
components = {
Hello: HelloComponent
};
render(createElement) {
return (<div id="app" className={appStyles}>
<img src="./assets/logo.png" />
<hello></hello>
</div>);
}
}
Output file
"use strict";
const typestyle_1 = require("typestyle");
const components_1 = require("./components");
const appStyles = typestyle_1.style({
fontFamily: 'Avenir, Helvetica, Arial, sans-serif',
'-webkitFontSmoothing': 'antialiased',
'-mozOsxFontSmoothing': 'grayscale',
textAlign: 'center',
color: '#2c3e50',
marginTop: 60,
});
class AppComponent {
constructor() {
this.name = 'app';
this.components = {
Hello: components_1.HelloComponent
};
}
render(createElement) {
return (createElement("div", { id: "app", className: appStyles },
createElement("img", { src: "./assets/logo.png" }),
createElement("hello", null)));
}
}
exports.AppComponent = AppComponent;
And the following change (remove createElement from arguments) would cause a compile error:
render() {
return (<div id="app" className={appStyles}>
<img src="./assets/logo.png" />
<hello></hello>
</div>);
}
Error message:
Christophers-MacBook-Pro:just-motion2 cwallis$ node ../typescript/built/local/tsc
src/App.tsx(19,18): error TS2304: Cannot find name 'createElement'.
src/App.tsx(20,18): error TS2304: Cannot find name 'createElement'.
src/App.tsx(21,18): error TS2304: Cannot find name 'createElement'.
I realize I have done this out of order. I decided to figure out how it could be done, and then saw the checklist while trying to put in a pull request. I have a commit to a local fork that has a working version of this: notoriousb1t@44ade7a. I have signed the CTA. I need to add some unit tests to that, but I am otherwise ready to submit a PR if this addition will be accepted.