Auto import the prop-types package when using PropTypes
Asked Answered
S

2

8

Not sure if this is the right community for it but it's worth a shot for me: I'm using PropTypes from the prop-types npm package. It's perfect for typing your React app but one thing that's really annoying for me is that my vs code editor doesn't recognise the import when i'm using PropTypes. It does make sense to me, since the package is called prop-types and you import it as a default import, most of the times named PropTypes, but i'm wondering if somebody has a nice trick to make this work with auto import in vs code, or any other way so I don't have to import it manually.

So.. when typing:

DiscoverAppsBanner.propTypes = {
  trackingContext: PropTypes
}

I would expect that the import would show up here:

image of propTypes

Anyone out there with a nice idea? :)

Subheading answered 17/11, 2021 at 13:24 Comment(1)
Have you tried with @types ?Farthingale
T
2

From my side, I resolve issue by a crazy trick, I create file with name propTypes.js and I import and export library in this file like this:

import PropTypes from 'prop-types';

export default PropTypes;

By this trick, auto import is work and refer to myfile, and I forget the anyone import.

enter image description here

and the import result import PropTypes from "propTypes";

Turcotte answered 29/11, 2021 at 13:44 Comment(2)
yes, as you can read in the bounty description we've done the same thing :) And that's fine for now, but if we get more libs that are having the same problem than we're nowhere unfortunately.Subheading
Oops, Sorry, I dont see that..., you are correct, the issue here its base on TS Api version for each library is used, so that the fix usually not from vs-code itself (in general), its from the library...I will follow this too...thank youTurcotte
F
0

prop-types is missing the type informations, you can add them with npm i @types/prop-types

Farthingale answered 6/12, 2021 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.