TS issue when importing a local options JSON file or JS Object
See original GitHub issueContact Details
No response
What do you need?
Hi there, I’m trying to load my option from a local JSON file, something like:
import options from "../../tsparticles/config.json";
<Particles options={options} />
It is working but I’m getting a ts error in the options property of <Particles />. I tried exporting a js object and the same error occurs:
(JSX attribute) options?: RecursivePartial<IOptions> | undefined
Type '{ autoPlay: boolean; background: { color: { value: string; }; image: string; position: string; repeat: string; size: string; opacity: number; }; backgroundMask: { composite: string; cover: { color: { value: string; }; opacity: number; }; enable: boolean; }; ... 12 more ...; zLayers: number; }' is not assignable to type 'RecursivePartial<IOptions>'.
The types of 'interactivity.detectsOn' are incompatible between these types.
Type 'string' is not assignable to type '"window" | InteractivityDetect | "canvas" | "parent" | undefined'.ts(2322)
index.d.ts(17, 5): The expected type comes from property 'options' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<IParticlesProps, IParticlesState, any>> & Readonly<...> & Readonly<...>'
Version
nextjs 12
Which library are you using?
React.js (react-tsparticles, react-particles-js)
Code of Conduct
- I agree to follow this project’s Code of Conduct
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Importing JSON file in TypeScript - Stack Overflow
With TypeScript 2.9.+ you can simply import JSON files with benefits like typesafety and intellisense by doing this: import colorsJson from ...
Read more >Importing JSON Modules in TypeScript - Marius Schulz
TypeScript 2.9 introduced a new --resolveJsonModule compiler option that lets us import JSON modules from within TypeScript modules.
Read more >How to Import a JSON file in TypeScript | bobbyhadz
To import a JSON file in TypeScript, set `resolveJsonModule` to `true` in your `tsconfig.json` file. Set `esModuleInterop` to `true` in `tsconfig.json`.
Read more >Import JSON file in code.ts - Figma Community Forum
One hack you can try is to turn the JSON file into a JavaScript file by adding export default at the top of...
Read more >How to import JSON files in ES modules (Node.js) - Stefan Judis
Option 1: Read and parse JSON files yourself. The Node.js documentation advises to use the fs module and do the work of reading...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It worked, thanks a lot.
I tested the config on a clean TS project. Try assigning it to a variable with
ISourceOptions
type (imported fromtsparticles
)