2

I'm trying to import an HTML file to render it inside of a react component.

I'm getting the following typescript error:

Cannot find module './privacy_policy.html' or its corresponding type declarations.ts(2307)

What could I do to get rid of it (other then use @ts-ignore)?

2
  • 2
    create a jsx file, and return the html in a functional componenet Commented Aug 3, 2022 at 18:24
  • What do expect importing an html file to do, exactly? Commented Aug 3, 2022 at 20:05

2 Answers 2

4

You can consider declaring a *.html module in a file named custom.d.ts in your project (at the same place where you defined your tsconfig.json).

For example, I've used this configuration on a web application (essentially Vanilla JS + TS for newer code) bundled with webpack, to handle import of types of file different from JS(x) or TS(x).

The alternative, as you said, is to use @ts-ignore approach when it's for a single specific need.

Note for this approach: in my case, the TypeScript transpilation is handled via webpack (with the babel-loader), and the imports as well are handled via webpack (via specific loaders, which for HTML is html-loader).

The html-loader lets you import a HTML file as it was a string in your TypeScript / JavaScript file.

Normally, if you use the ReactApp template, you should have the html-loader included in the build pipeline (which runs webpack behind the scene), so you should be able to use it.

Here is how it looks my custom.d.ts file:

/* 
 * Tells the IntelliSense to allow import of the following file extensions in TypeScript.
 * Current Webpack config for these files doesn't embed their content, but provides the file path inside the Webpack bundle.
 */

declare module "*.svg" {
    const content: string;
    export default content;
}
declare module "*.png" {
    const content: string;
    export default content;
}
declare module "*.html" {
    const content: string;
    export default content;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I ended up doing. Thank you.
0

You have to transform your HTML code to JSX (or TSX if you are using Typescript). Check online, there are few tools that might help you !

Then you will be able to import your HTML (converted to JSX) into your script:

import PrivacyPolicy from 'privacy-policy.jsx'

return (
    <PrivacyPolicy />
)

4 Comments

Since this is an external file, generated by an external service I prefer to consume it as is. I'm rendering it on a page using dangerouslySetInnerHTML. It works fine, but I'd love to get rid of that ts error.
If the external service has web server, you could use iframe to add external HTML to your site
It's one of those privacy policy generators. All they give you on the free tier is an html snippet to copy. I have to serve it myself.
I found the solution here: stackoverflow.com/a/50794851/11730250 Tell me if it works !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.