styled-components: Invalid path to TypeScript types in styled-components/native

"styled-components": "^6.1.14"

node_modules/styled-components/native/package.json contains the following entry:

{
  "name": "styled-components/native",
  "private": true,
  "types": "./dist/native/index.d.ts",
  "main": "./dist/styled-components.native.cjs.js",
  "jsnext:main": "./dist/styled-components.native.esm.js",
  "module": "./dist/styled-components.native.esm.js"
}

However, the types field points to a non-existent file (./dist/native/index.d.ts), as shown in the attached screenshot:

Image

It seems like the types are bundled into ./dist/dist/native/index.d.ts instead. This results in TypeScript errors when importing styled-components/native, such as:

Could not find a declaration file for module 'styled-components/native'. '.../node_modules/styled-components/native/dist/styled-components.native.cjs.js' implicitly has an 'any' type.ts (7016)

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 11
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

✅ Solution Found: Issue Resolved

I was able to fix the issue by making the following changes:

Removed the @types/styled-components-react-native package:

npm uninstall @types/styled-components-react-native

Since the latest version of styled-components already includes built-in TypeScript support, this package was unnecessary and was likely causing conflicts.

Updated styled-components to version 6.1.13 (without ^): In package.json, I changed:

styled-components": "6.1.13"

Then I reinstalled dependencies:

npm install

After these changes, the TypeScript errors related to ‘theme’ implicitly has an ‘any’ type were resolved, and everything is now working correctly. 🚀

Hope this helps others facing the same issue! 🎉

Hello, How are you?!

I have an problem with that. I reverted to the previous version “styled-components”: “6.1.13”

Instead of “styled-components”: “6.1.14”

It worked for me

including solving global theme type problem to me.

my discord Gabriel_Jesus#9668

This seems to be a an issue introduced in 6.1.14 Will probably need a proper fix instead of the workarounds mentioned before.

What worked in our case was to add to add both styled-components 6.1.13 and @types/react 18.3.4 to resolutions

  "resolutions": {
     "styled-components": "6.1.13",
     "@types/react": "18.3.4"
  }

I resolved the issue by adding the following to the tsconfig.json file:

"compilerOptions": {
    "types": ["styled-components-react-native"],
 },

After making this change, I installed the styled-components typings, and the error no longer appeared.

I’m now using the default import, and everything seems to be working as expected:

import styled from 'styled-components';

Any drawbacks?

That has always worked as far as I can tell, but if you want to use any of react native’s elements in styled components the only way is to do styled(Text) instead of styled.Text which means more imports in your file (there may be more differences, but that’s the only one I know), and even then I don’t know if it works properly or not…

@djvistasa you put the ^ in front of the version in your package.json. That means the 6.1.14 is fine for your package manager.

Remove the ^ and make sure that .lock file contains only references to 6.1.13.

"styled-components": "6.1.13", // Removed ^

This will force the version to stay on 6.1.13.

You can try also adding resolutions, if that doesn’t work

"resolutions": {
  "styled-components": "6.1.13"
}