danalloway / react-country-flag

React component for emoji/svg country flags.

Home Page:https://danalloway.github.io/react-country-flag/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Declaration File missing

jpmaligne opened this issue · comments

What I have done : npm install --save react-country-flag

react-country-flag folder appears well in my node-modules.

Then : import ReactCountryFlag from "react-country-flag"; from a .ts file

Gets me this error :

Could not find a declaration file for module 'react-country-flag'. '/home/{...}/node_modules/react-country-flag/dist/index.js' implicitly has an 'any' type.
  Try `npm install @types/react-country-flag` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-country-flag';`  TS7016

that is correct, I do not have any type declarations for this project

I would recommended following their suggestion and adding a local type declaration for this library in your project

commented
declare module 'react-country-flag' {
  import * as React from 'react';
  export interface ReactCountryFlagProps {
    code: string;
  }
  export default class ReactCountryFlag extends React.Component<ReactCountryFlagProps, any> {}
}
`
``

@webhacking if you submit a PR with this I'll send it through

@danalloway @webhacking Is there something new about declaration file? I can create PR if you want.

@thibaultboursier want to wait for v2 to land, then create a PR for that one?

v2 is out, ready for that declaration file if you're still up for it

Ok, i'll doit that soon.

@thibaultboursier just checking in on this, if you're still going to do it i'll leave this issue open, otherwise please close it out for me, thank you

@danalloway Yes, still going to do it.

@thibaultboursier @danalloway - Just wanted to ping to see if this is still happening?

@thibaultboursier @danalloway - Just wanted to ping to see if this is still happening?

Yes, I still getting this error, where is the .ts file?

@DATeclipse @FabianoLothor waiting on a PR for this, feel free to take a stab at it I don't know if anyone else is at the moment

@danalloway I fix it in my project with this workaround:

// New file /src/external.d.ts
declare module "react-country-flag";

Here are some declarations i wrote for v2. It's not perfect, but it seems to work for me. Maybe someone who is better at TS can come along and make it perfect.

declare module 'react-country-flag' {
  import * as React from 'react';
  export interface ReactCountryFlagProps<T> extends React.DetailedHTMLProps<React.LabelHTMLAttributes<T>,T> {
    cdnSuffix?:string;
    cdnUrl?:string;
    countryCode: string;
    svg?: boolean;
    style?: React.CSSProperties;
  }
  const ReactCountryFlag:React.FC<ReactCountryFlagProps<HTMLSpanElement|HTMLImageElement>> = ({
    cdnSuffix='svg',
    cdnUrl='https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.4.3/flags/4x3/',
    svg=false,
    style={},
    countryCode
  })=>React.ReactNode;// return should be HTMLImageElement or HTMLSpanElement but close enough for me.
  export default ReactCountryFlag;
};

Hello @danalloway @FabianoLothor @r3wt,

I opened a PR to add TypeScript and generate declaration files each time project is built.
You can check it here.

@thibaultboursier nice work! the only thing is, i'm not sure how the repo owner will feel about it, as it might make it difficult to maintain if they don't know typescript.

@r3wt Thanks. Let's discuss with @danalloway about that :-) Anyway, with a declaration file manually written, we would also have to maintain it with TypeScript, when codebase evolves.

@r3wt @thibaultboursier I may prefer the manual / static file
this code base won't change much as it's a pretty dead simple library
will update you all ASAP when I make the change

Any update on this?

@danalloway I fix it in my project with this workaround:

// New file /src/external.d.ts
declare module "react-country-flag";

tnx!!

working on a Typescript version of this library, should publish today

just released version v3.0.1 with Typescript support