I have some .ts
files that has .jsx syntax in them. I want to tell tsc
to compile my these .ts files like compiling .tsx files.
Is is possible to configure tsc
to do that?
How can I configure vscode to support syntax highlighting for JSX syntax in these files?
ts files still support the casting form which will cause conflicts if you'll have jsx code in them.
If just running tsc doesn't work, read this code.visualstudio.com/docs/typescript/typescript-compiling Effectively you can just compile it into Javascript. If the output of tsc is too much, just go through the tsx files and remove TS specific syntax, then rename to . jsx.
A TSX file is a TypeScript (. TS) file written using JSX syntax. It contains code that is most likely part of a single-page or mobile application. TSX files can be opened in any text editor, but are meant to be opened in source code editors.
React uses a special syntax that resembles HTML to declare components. This markup, called JSX, is embedded in the component JavaScript and needs to be compiled to JavaScript before it's usable by the browser.
No, you must have your react code in .tsx
files, as the docs say:
In order to use JSX you must do two things.
- Name your files with a .tsx extension
- Enable the jsx option
The reason for this is that in typescript you cast in this form:
interface Point {
x: number;
y: number;
}
let p = <Point> {};
In tsx
files however this is not possible because <Point>
can be markup for a Point
component.
Because of that in tsx
files casting is not possible and you need to type assert:
let p = {} as Point;
But regular .ts
files still support the casting form which will cause conflicts if you'll have jsx code in them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With