avishayil / react-native-user-avatar

Avatar Component For React Native
MIT License
206 stars 57 forks source link

Typescript support #114

Open lyqht opened 2 years ago

lyqht commented 2 years ago

When i install the library in a Typescript project, I get the following error

Could not find a declaration file for module 'react-native-user-avatar'. '/Users/lyqht/project/node_modules/react-native-user-avatar/lib/module/index.js' implicitly has an 'any' type.
tdammy92 commented 2 years ago

I normally get the same typescript error.

i just do //@ts-ignore

pablolucio97 commented 9 months ago

To type react-native-user-avatar you can do:

1 - Create a folder named @types inside your .src directory, and a file named userAvatar.d.ts inside it.

2 - Declare the 'react-native-user-avatar' module typing it as:

declare module 'react-native-user-avatar' {
  const content: {
    name: string;
    src?: string;
    bgColor?: string;
    bgColors?: string[];
    textColor?: string;
    size?: number;
    imageStyle?: object;
    textStyle?: object;
    style?: object;
    borderRadius?: number;
    component?: React.ComponentType;
  };
  export default content;
}

3 - Inside your tsconfig.json file, add this line to tell Typescript where to find the declaration typing file for the library:

"typeRoots": ["./node_modules/@types", "./@types"]

4 - Restart your IDE.

Now your problem should be gone. Happy coding.