[Box] TS error when assiging a typed ref to Box
See original GitHub issueTyped ref on Box component
Describe the bug
When using the box component and trying to pass a typed react ref according to as
prop, it complains about type mismatch since Box
component is not generic
To reproduce
pass a typed ref to Box
component
const boxRef = React.useRef<HTMLSpanElement>(null);
...
<Box as="span" ref={boxRef}>
TS error: Type 'RefObject<HTMLSpanElement>' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'. Type 'RefObject<HTMLSpanElement>' is not assignable to type 'RefObject<HTMLDivElement>'. Property 'align' is missing in type 'HTMLSpanElement' but required in type 'HTMLDivElement'.
Minimal reproduction
https://codesandbox.io/s/box-typed-ref-27mkm
Expected behavior
Maybe make Box
generic so we can type it, something like this: <Box<HTMLSpanElement> />
, not sure if it can take the type from as
prop 🤔
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Argument of type 'X' is not assignable to parameter of type 'X'
The Error message here is because the Square brackets for Array Initialization is missing, It works even without it, but VS Code red...
Read more >Documentation - Object Types - TypeScript
Cannot assign to 'resident' because it is a read-only property.Cannot assign to 'resident' because it is a read-only property.
Read more >Using React Refs in Typescript - Pluralsight
It simply tests that the ref has been set - the Typescript compiler will report an error if the if statement is omitted...
Read more >typescript-cheatsheet - GitHub Pages
In TypeScript any argument of a function may be assigned a type no matter how ... React provides the reference objects types out...
Read more >API Reference - styled-components
API Reference of styled-components. ... Prop Value, These can be of any type, except functions. They'll stay static and will be ... const...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Keep this open please. This remains a shortcoming.
I’ve just hit this issue. As a workaround I’ve modified some type declarations in @chakra-ui/system/dist/types/system.types.d.ts:
It now infers the correct type declarations for
ref
and other props using Box, but I’m not sure if would cause any problems with the rest of the library. I’ll be making a pull request to test this changesAlso, I agree this issue should be reopened