I'd like to use JSX syntax in TypeScript, but I don't want to use React.
I have seen answers to other related questions here on SO, but nothing was complete or detailed enough to be of any help. I've read this guide and the JSX chapter of the handbook, and it didn't help much. I don't understand how the language-feature itself really works.
I've tried to examine the React declarations, but it's just too huge for me to absorb - I need a minimal, working example demonstrating type-checked elements, components and attributes. (It doesn't need to have a working implementation of the factory-function, I'm mostly interested in the declarations.)
What I'd like, minimally, is an example that makes the following work, with type-safety:
var el = <Ping blurt="ya"></Ping>;
var div = <div id="foo">Hello JSX! {el}</div>;
I assume I'll need to declare JSX.IntrinsicElements at least, and a createElement() factory-function of some sort, but that's about as far as I got:
declare module JSX {
interface IntrinsicElements {
div: flurp.VNode<HTMLDivElement>;
}
}
module flurp {
export interface VNode<E extends Element> {
id: string
}
export function createElement<T extends Element>(type: string, props?: any, ...children: (VNode<Element>|string)[]): VNode<Element> {
return {
id: props["id"]
};
}
}
class Ping {
// ???
}
var el = <Ping blurt="ya"></Ping>;
var div = <div id="foo">Hello JSX! {el}</div>;
I compile this with tsconfig.json like:
{
"compilerOptions": {
"target": "es5",
"jsx": "react",
"reactNamespace": "flurp"
}
}
Hovering over the <div> element and id="foo" attribute, I can see the compiler understands my declaration of intrinsic elements - so far, so good.
Now, to get the <Ping blurt="ya"> declaration to compile, with type-checking for the blurt attribute, what do I do? Should Ping even be a class or is it maybe a function or something?
Ideally, elements and components should have a common ancestor of some sort, so I can have a common set of attributes that apply to both.
I'm looking to create something simple and lightweight, perhaps along the lines of monkberry, but with a JSX syntax, e.g. using components like <If> and <For> rather than inline statements. Is that even feasible? (Is there any existing project along those lines I can look at for reference?)
Please, if anyone can provide a working, minimal example of how to use the language feature itself, that would be a huge help!