mathlive icon indicating copy to clipboard operation
mathlive copied to clipboard

Type error when trying to use <math-field> element in React + TypeScript project

Open JohnHoward9 opened this issue 3 years ago • 2 comments

Include code fragments or CodePen.io links to illustrate the issue. Do not include screenshots of code.

` // App.tsx import "//unpkg.com/mathlive"; import './App.css'; import { useState } from "react";

function App() { const [value, setValue] = useState("");

return (

<math-field onInput={evt => setValue(evt.target.value)} > {value}

Value: {value}

); }

export default App;`

Description

I'm trying to get a basic TypeScript React App working with MathLive. All is ok when using JavaScript, but I get the following error when using TypeScript.

Property 'math-field' does not exist on type 'JSX.IntrinsicElements'.ts(2339)

Steps to Reproduce

Vanilla React with typescript template with the following App.tsx

// App.tsx import "//unpkg.com/mathlive"; import './App.css'; import { useState } from "react";

function App() { const [value, setValue] = useState("");

return (

<math-field onInput={evt => setValue(evt.target.value)} > {value}

Value: {value}

); }

export default App;`

Actual Behavior

Error a build time

Expected Behavior

There was a previous error reported 'Type error when trying to use element in React + TypeScript project', but that developer was using the wrong field name.

Environment

Is this a regression: did it use to work in a previous version?

MathLive version

0.96.2

Operating System

Windows 11

Browser Chrome, Edge

Thanks in advance for any help :)

JohnHoward9 avatar Nov 20 '23 13:11 JohnHoward9

The error you're encountering, "Property 'math-field' does not exist on type 'JSX.IntrinsicElements'", is a common issue when using custom elements in a TypeScript React application. This is because TypeScript expects properties on JSX elements to be predefined, which is not the case for custom elements like math-field from MathLive.

To resolve this issue, you can extend the JSX.IntrinsicElements interface to include the custom elements you're using. Here's how you can modify your code to fix the error:

  1. Define Custom Element Types: You need to declare the type for your custom element. This can be done by extending JSX.IntrinsicElements.

  2. Import React Correctly: Ensure you're importing React correctly to use JSX.

  3. Update the math-field Element: Modify the math-field element to comply with the TypeScript syntax.

Here's an updated version of your App.tsx:

import React, { useState } from "react";
import "//unpkg.com/mathlive";
import './App.css';

// Extend JSX.IntrinsicElements
declare global {
  namespace JSX {
    interface IntrinsicElements {
      "math-field": React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
    }
  }
}

function App() {
  const [value, setValue] = useState("");

  return (
    <div>
      <math-field
        onInput={(evt: any) => setValue(evt.target.value)}
      >
        {value}
      </math-field>
      <p>Value: {value}</p>
    </div>
  );
}

export default App;

Explanation:

  • The declare global section extends JSX.IntrinsicElements to include math-field. This tells TypeScript that math-field is a valid JSX element.
  • The onInput handler's event is typed as any for simplicity. You might want to replace any with a more specific type based on your application's needs.
  • The rest of the component remains largely the same as your original code.

After making these changes, your TypeScript React app should be able to compile and run without the mentioned error.

arnog avatar Nov 20 '23 15:11 arnog

Thanks arnog, that worked perfectly :)

JohnHoward9 avatar Nov 21 '23 09:11 JohnHoward9