Typescript image import


Typescript image import



I found a solution here: Webpack & Typescript image import



But i am getting error for this:



[ts]
Types of property 'src' are incompatible.
Type 'typeof import(".png")' is not assignable to type 'string | undefined'.
Type 'typeof import("
.png")' is not assignable to type 'string'.



I guess i need to cast import somehow, but cant figure out how.
I am doing this in React. I saw that src attribute is defined as string | undefined, that is why error is popping.


src


string | undefined



Here is code:


import * as Logo from 'assets/images/logo.png';



HTML:


<img src={Logo} alt="" />



And definition based on above mentioned solution:


declare module "*.png" {
const value: any;
export default value;
}



Tsconfig:


{
"compilerOptions": {
"baseUrl": "./",
"jsx": "react",
"lib": ["es5", "es6", "dom"],
"module": "commonjs",
"noImplicitAny": false,
"outDir": "./dist/",
"sourceMap": true,
"strictNullChecks": true,
"target": "es5",
"typeRoots": [
"custom_typings"
]
},
"include": ["./src/**/*.tsx"],
"exclude": ["dist", "build", "node_modules"]
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV