React Native TypeScript Images & Static Assets

前端 未结 2 1691
天命终不由人
天命终不由人 2021-02-14 12:36

I was following along in the tutorial that Microsoft put out on how to convert your ReactNative project to TypeScript.

Everything worked great until I reached the point

2条回答
  •  故里飘歌
    2021-02-14 13:15

    I'am not sure if this is the best approach,the way I solved it is,

    Create a file in the root folder containing images(Should be read by typescript) or any other folder as per your folder structure. Name it "your-name-here.d.ts" (d.ts should be the extension.Its used by typescript engine)

     declare module '*.jpg' {
      import { ImageSourcePropType } from 'react-native';
      const value: ImageSourcePropType;
      export default value;
    }
    

    ImageSourcePropType is the type expected by the source argument in Image

    Now where ever an image is used can be used as

    import SampleIcon from '../images/sample-image.jpg';
    
    
    

提交回复
热议问题