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:
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
- KAN-90 Chore: styled-components 설치, 테마/타입 구현 styled-components 최신 버전 (6.1.14)에서 타입을 읽지 못하는 문제 존재해 6.1.13으로 강제 고정시켜 해결 https://github.com/styled-components/styled-components/issues/5516 — committed to team-Melissa/melissa-FE by NARARIA03 2 years ago
- build: :pushpin: Make sure we are using 6.1.13 until there's a fix for https://github.com/styled-components/styled-components/issues/5516 — committed to refugies-info/karfur by kaaloo 2 years ago
✅ 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-nativeSince 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 installAfter 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
I think this is fixed in the most recent release https://github.com/styled-components/styled-components/releases/tag/styled-components%406.1.16
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-components6.1.13 and@types/react18.3.4 to resolutionsI resolved the issue by adding the following to the
tsconfig.jsonfile:After making this change, I installed the styled-components typings, and the error no longer appeared.
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 ofstyled.Textwhich 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 yourpackage.json. That means the 6.1.14 is fine for your package manager.Remove the
^and make sure that.lockfile contains only references to 6.1.13.This will force the version to stay on 6.1.13.
You can try also adding resolutions, if that doesn’t work