Type error when trying to use <math-field> element in React + TypeScript project
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 (
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 (
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
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 :)
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:
-
Define Custom Element Types: You need to declare the type for your custom element. This can be done by extending
JSX.IntrinsicElements. -
Import React Correctly: Ensure you're importing React correctly to use JSX.
-
Update the
math-fieldElement: Modify themath-fieldelement 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 globalsection extendsJSX.IntrinsicElementsto includemath-field. This tells TypeScript thatmath-fieldis a valid JSX element. - The
onInputhandler's event is typed asanyfor simplicity. You might want to replaceanywith 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.
Thanks arnog, that worked perfectly :)