Is there any downside to using .tsx instead of .ts all the times in typescript?

后端 未结 5 934
长情又很酷
长情又很酷 2020-12-22 16:54

I just start working on a React project with TypeScript and ask myself what should I do with regular class files? Should I use .ts or .tsx files an

5条回答
  •  萌比男神i
    2020-12-22 17:22

    It's kind of a convention to use x in the end when your JavaScript is in JSX Harmony mode. That is, when this is valid:

    doSomething(
    My div
    );

    However, your file extension doesn't really matter, as long as your pre-processors are aware of your decision (browserify or webpack). I, for one, use .js for all my JavaScript, even when they are React. The same applies for TypeScript, ts/tsx.

    EDIT

    Now, I would strongly recommend using JSX for Javascript with React syntax and TSX for TypeScript with React because most editors/IDEs will use the extension to enable or not the React syntax. It is also consider it to be more expressive.

提交回复
热议问题