11

I tried to use typescript with jsx without react. and it doesn't work. I always put a "jsx": "preserve" in tsconfig.json. But i dont understand what i have to do next.and when i am compiling .tsx file webpack throws an error

ERROR in ./core/Navbar.tsx Module parse failed: /home/ubuntu/Desktop/framework/node_modules/ts-loader/index.js!/home/ubuntu/Desktop/framework/core/Navbar.tsx Unexpected token (9:15)

You may need an appropriate loader to handle this file type. i read the documentation but i dont understand how to use global jsx module in project. is it possible to use typescript + jsx without react?

enter image description here my App.tsx class

[my App.tsx class[2]

error when webpack compile file console error

0

3 Answers 3

17

Use tsconfig reactNamespace option:

// tsconfig.json
{
  "compilerOptions": {
    "jsx": "react",
    "reactNamespace": "JSX",
  }
}

Define the JSX namespace with a createElement function:

// index.tsx
export const JSX = {
  createElement(name: string, props: { [id: string]: string }, ...content: string[]) {
    props = props || {};
    const propsstr = Object.keys(props)
      .map(key => {
        const value = props[key];
        if (key === "className") return `class=${value}`;
        else return `${key}=${value}`;
      })
      .join(" ");
      return `<${name} ${propsstr}> ${content.join("")}</${name}>`;
  },
};

export default JSX;

declare the types

// external.d.ts
declare module JSX {
  type Element = string;
  interface IntrinsicElements {
    [elemName: string]: any;
  }
}

Usage example

import JSX from "./index";

function Hello(name: string) {
    return (
        <div className="asd">
            Hello {name}
            <div> Hello Nested </div>
            <div> Hello Nested 2</div>
        </div>
    );
}

function log(html: string) {
    console.log(html);
}

log(Hello("World"));
Sign up to request clarification or add additional context in comments.

1 Comment

Isn't that implemention missing proper escaping, making it prone to injection attacks?
3

If you use jsx: prevserve, it means that the Typescript compiler will output .jsx files rather than compiling it down to .js, as a result, you will need something like Babel to transpile your jsx eventually since your browser can't run jsx files.

To be honest, I'm not sure what you are trying to do, but you should either use jsx: react, or jsx: preserve + transpiler

14 Comments

thanx for ur answer. i understand) so i need to transpile my jsx after tsx compilation to jsx. but how can i do that? i am using webpack. and what loaders i need? and how to run jsx transpiler after ts-loader?
can u show a valid webpack config for compiling typscript + jsx?
i think i have to chain loaders ts-loader!babel right?
yes, you need to chain loaders, but it would be babel-loader!ts-loader, you want the output of ts compiler r into babel
if you use babel-loader together with ts-loader then you want to have JSX: preserve; not react
|
1

Look at Stefan Baumgartners blog entry "JSX is syntactic sugar". It explains how to convert JSX to DOM elements at runtime using clean, easily readable code.

1 Comment

There's a package with the same idea. npmjs.com/package/jsx-dom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.