Is it advisable to implement preset-react alongside preset-typescript when the jsx option is configured for 'react'?

Utilizing babel along with preset-typescript for transpiling react tsx files. According to the documentation, when the jsx option is configured as react, preset-typescript will convert <div /> to

React.createElement("div")
and generate a js file. Given this process, is it still necessary to include preset-react, which seemingly performs the same function?

Answer №1

Is there a jsx option missing? Perhaps you meant to refer to jsxPragma in this preset or the jsx setting in the tsconfig.json file?

Also, does @babel/preset-typescript currently have that capability? I attempted to use Babel to transpile a single file named index.tsx, which includes elements like <div>...</div>. Even with all relevant settings applied, nothing seemed to happen to the JSX element <div>...</div> until I included @babel/preset-react.

I believe the mentioned option is intended for informing Babel that React imports should not be treated as type imports, rather than removing JSX expressions as outlined in the preset-typescript documentation.

Below is the initial Babel configuration I utilized:

{
  "presets": [
    "@babel/env",
    [
      "@babel/preset-typescript",
      {
        "isTSX": true,
        "allExtensions": true,
        "jsxPragma": "React",
        "jsxPragmaFrag": "React.Fragment"
      }
    ]
  ]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the keys of a type by analyzing one of its values

My global type definition consists of an object with keys (referred to as a prop) that must be passed by the user as a parameter to myFunction(), and values that are used solely for type checking within my code. These values can fall into only 3 "possible ...

Retrieving the value of a variable within an object using an Observable

Can someone help me figure out how to assign a variable to a value inside an object in an Observable in my typescript file? I can retrieve the variable's value in my HTML file, but I seem to be missing something crucial. I believe the solution may inv ...

How can I conceal an element upon page load using Ionic framework?

index.component.ts initialize() { // Execute necessary functions after loading this.index.ready().then(() => { if(this.index.is('core')){ // this.menu.enable(false, 'mobileOnlyPages'); }else{ ...

Issue: Module 'typescript' not found in Ionic application

As a beginner in the world of Ionic framework, I encountered a problem while attempting to build my app using "ionic serve" - I received the error message "cannot find module 'typescript'". I believed I had resolved the issue by installing Ty ...

Tips for including extra space before or after '{' and '}' in WebStorm?

While I have some experience with JetBrains products and setting styles for my files, I am struggling to configure my .tsx file to display the desired style. It appears that changes made in the TypeScript section are not affecting my .tsx file. In the ima ...

Is there a way to stop WhatsApp Web from launching in a new tab if another WhatsApp Web tab is already open?

How can I prevent the WhatsApp Web URL from opening in a new tab if there is already a WhatsApp Web tab open? const msg = "*Greetings From "+this.previewData.unit_name+" "+this.previewData.unit_location+"* "; const full = `$ ...

Using TypeScript for abstract methods and polymorphism

What do I need to fix in order for this code to function properly? abstract class Animal { // No constructor ... public abstract me():Animal; } class Cat extends Animal { constructor() { super(); } // Why does this no ...

Struggling to compile Typescript with mocha using the Visual Studio Code Debugger

I'm currently troubleshooting unit testing using Visual Studio Code and mocha, but I encounter an error when mocha is launched. TSError: ⨯ Unable to compile TypeScript: mfa/test/index.test.ts(4,20): error TS2307: Cannot find module 'assert&ap ...

Trouble with Firebase cloud functions following the transition to TypeScript

Recently, I made an attempt to transition my firebase cloud functions from JavaScript to TypeScript and organized them into separate files. However, I encountered persistent errors while trying to deploy and serve the functions: Errors during serving: fu ...

Ensure that parameters are validated correctly in the Next.JS application router using the searchParams method

When building the page, I need to properly validate params in the Next.JS app router using searchParams. My goal is to show a main image (coverImage) for each photo on the /gallery page. When a photo is clicked, I want to display more photos of the same k ...

Developing a custom package containing personally crafted type definitions and importing them into the project

I'm in need of creating a private npm package specifically for custom type definitions (typedefs) that are hand-written d.ts files not generated by TypeScript. Since these are proprietary, they cannot be added to DefinitelyTyped. The folder structure ...

The debounced function in a React component not triggering as expected

I am facing an issue with the following React component. Even though the raiseCriteriaChange method is being called, it seems that the line this.props.onCriteriaChange(this.state.criteria) is never reached. Do you have any insights into why this.props.onC ...

What is the method for displaying script commands within package.json files?

With a multitude of repositories, each one unique in its setup, I find myself constantly referencing the package.json file to double-check the scripts. "scripts": { "start": "npm run dev" "build:dev": "N ...

How can you enhance a component by including additional props alongside an existing onClick function?

As a newcomer to React and TypeScript, I've created a simple component that looks like this: const CloseButton = ({ onClick }: { onClick: MouseEventHandler }) => { const classes = useStyles(); return <CloseIcon className={classes.closeButto ...

Is there a way to divide v-progress linear into 4 pieces in Vuejs, or are there alternative design options for achieving this in Vuetify 2?

I have set up a table in Vuetify 2 with a v-progress-linear component to monitor the user's remaining time. Initially, my implementation was simple like this. https://i.sstatic.net/x373G.png However, we decided to split it into 4 sections for better ...

Change the keys of the object in the return statement

Scenario Imagine a scenario where a method called matter is returning an object in the form of return {content, data} Issue There is a conflict when the method is called a second time, as it overwrites any previous variables that were set from the return ...

Expanding function parameter types using intersection type in Typescript

As I delve into the world of intersection types to enhance a function with an incomplete definition, I encountered an interesting scenario. Take a look at this code snippet: WebApp.connectHandlers.use("/route", (req:IncomingMessage, res:ServerResponse)=& ...

Utilize NgOnChange to detect changes in an @Input property and display it on a template as undefined. Consider using a set method instead

Currently, I am facing an issue where I am utilizing two-way data binding to call a function in my template. template.html {{arePresent()}} This function essentially checks if I have a @Input variable named length of type string [] component.ts @Input i ...

What is the best way to exclude multiple properties from an object in JavaScript?

Having two methods that return Pick<T, K> and Omit<T, K> types where Omit is defined as type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>, I am facing difficulty in removing multiple properties from an object. Th ...

Restricting the data type of a parameter in a TypeScript function based on another parameter's value

interface INavigation { children: string[]; initial: string; } function navigation({ children, initial }: INavigation) { return null } I'm currently working on a function similar to the one above. My goal is to find a way to restrict the initi ...