One of the ways to get rid of that error is by modifying d.ts file as follows:
declare module "*.png"
remove
{
const value: string;
export default value;
}
or alternatively you can do:
declare module "*.png" {
const value: any;
export default value;
}
Update
The best solution with type-checking is:
declare module "*.png" {
const value: any;
export = value;
}
Answer from Piyush Zalani on Stack OverflowOne of the ways to get rid of that error is by modifying d.ts file as follows:
declare module "*.png"
remove
{
const value: string;
export default value;
}
or alternatively you can do:
declare module "*.png" {
const value: any;
export default value;
}
Update
The best solution with type-checking is:
declare module "*.png" {
const value: any;
export = value;
}
For react-native
create global.d.ts file on project root folder and just add next lines there
declare module '*.png' {
const value: import('react-native').ImageSourcePropType;
export default value;
}
import images
How to import image from Public folder as a Typescript module?
Is there a less manual way of loading a bunch of images into an array?
reactjs - Importing images in TypeScript React - "Cannot find module" - Stack Overflow
Videos
I've done this:
import hangman0 from './assets/hangman0.png'; import hangman1 from './assets/hangman1.png'; import hangman2 from './assets/hangman2.png'; import hangman3 from './assets/hangman3.png'; import hangman4 from './assets/hangman4.png'; import hangman5 from './assets/hangman5.png'; import hangman6 from './assets/hangman6.png'; const hangman = [hangman0, hangman1, hangman2, hangman3, hangman4, hangman5, hangman6];
But I don't like it, because the whole point of coding is to automate repetitive tasks. So how would I go about writing something that could import an arbitrary folder of images into an array?
Create an index.d.ts file in the folder src, and add this line:
declare module '*.jpg';
If you literally wrote "include": ["./src/index.d.ts"] in tsconfig.json and you don't have a "files" setting, that means the project defined by tsconfig.json includes only the single file ./src/index.d.ts. When you open any other file in VS Code, VS Code uses a separate language service instance that doesn't use your tsconfig.json. Adjust your "include" setting to match all the .ts and .tsx files in your project, or just delete it and rely on the default behavior of including all files under the directory containing tsconfig.json.
Round 2
TypeScript is ignoring index.d.ts because it assumes that index.d.ts is generated from index.tsx and index.tsx is more likely to be up to date. Name your index.d.ts file something else, e.g., declaration.d.ts.
How can I get rid of the following error:
The image exists and the code words but I always have this error and I am tired of adding "@ts-ignore". This is happening in a React project created with CRA that uses the following jsconfig.json file:
{
"compilerOptions": {
"types": ["jest"],
"lib": ["ES2021", "DOM"],
"downlevelIteration": true,
"strict": true,
"jsx": "react",
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"checkJs": true
},
"exclude": [
".git",
".app-cache",
".npm",
".npm-tmp",
"dist",
"dist*",
"node_modules",
"subfolder/dist",
"subfolder/node_modules",
"**/dist/*",
"**/node_modules/*"
]
}
The problem is that you confuse TypeScript level modules and Webpack level modules.
In Webpack any file that you import goes through some build pipeline.
In Typescript only .ts and .js files are relevant and if you try to import x from file.png TypeScript just does not know what to do with it, Webpack config is not used by TypeScript.
In your case you need to separate the concerns, use import from for TypeScript/EcmaScript code and use require for Webpack specifics.
You would need to make TypeScript ignore this special Webpack require syntax with a definition like this in a .d.ts file:
declare function require(string): string;
This will make TypeScript ignore the require statements and Webpack will be able to process it in the build pipeline.
Instead of:
import image from 'pathToImage/image.extension';
Use:
const image = require('pathToImage/image.extension');