Closed
Description
Check that this is really a bug
- I confirm
Reproduction link
https://codesandbox.io/s/swiper-typescript-types-errors-437w68
Bug description
Using Swiper with Typescript it throws errors in the ref
and onSwiper
props.
Expected Behavior
It should not throw an error even if types are assigned to it.
Actual Behavior
No response
Swiper version
8.0.6
Platform/Target and Browser Versions
macOs bigsur Chrome lastest
Validations
- Follow our Code of ConductRead the docs.Check that there isn't already an issue that request the same feature to avoid creating a duplicate.Make sure this is a Swiper issue and not a framework-specific issue
Would you like to open a PR for this bug?
- I'm willing to open a PR
Activity
rtabulov commentedon Feb 28, 2022
hey, this is not a problem with Swiper or Typescript, i would say this is not a problem at all
ref
is not supported for function components in reactonSwiper
shows error becuase types null and Swiper are not compatible, that's because you did not properly set the type onuseState
to do that you should write
youngkyo0504 commentedon Feb 28, 2022
Do you want access swiper container??
then do this.
vicasas commentedon Feb 28, 2022
@rtabulov The
ref
makes sense. But this means that Swiper should still be able to receive the ref without Typescript throwing an error. Perhaps usingfordwarRef
.Thinking out loud, you should consider adding last name
Props
to types. e.g.SwiperProps
Tends to be confusing without using
import type
vicasas commentedon Feb 28, 2022
@youngkyo0504 Taking that same code to my example in codesandbox how would it be? Using
fordwarRef
?SiJBC commentedon Apr 13, 2022
//@ts-ignore
onSwiper ={setThumbsSwiper}
it's a stranded friends last option it worked for me on this same issue - sometimes need to find quick workarounds - cannot see any situation where a type check would help to ensure the right function was being passed in
TondeMoon commentedon May 11, 2022
this is also can be avoided by -
const [thumbsSwiper, setThumbsSwiper] = useState<any>(null);
its Swiper 6.8
TomSmedley commentedon Sep 29, 2022
This is what I had to do with Swiper and React
Then
And
Why there is a component called Swiper and a Interface called Swiper is beyond me! Really
swiper/react
needs an alias forSwiper
interface fromswiper
package in my opinion.vicasas commentedon Sep 29, 2022
That's another "issue" that types have the same name as components and one must end up adding aliases to fix the name clash. All component types should be renamed to
[Component]Props
where possible.nolimits4web commentedon May 15, 2023
This is correct usage from 9.3.2
feat(react): export `SwiperClass` type from 'swiper/react'