avishayil/react-native-user-avatar

Typescript support

lyqht opened this issue · 2 comments

lyqht commented

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.

I normally get the same typescript error.

i just do //@ts-ignore

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.