question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Box] TS error when assiging a typed ref to Box

See original GitHub issue

Typed 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
chinandermcommented, Jul 28, 2021

Keep this open please. This remains a shortcoming.

0reactions
santiagocezarcommented, Oct 27, 2021

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:

export declare type MergeWithAs<ComponentProps extends object, AsProps extends object, AdditionalProps extends object = {}, AsComponent extends As = As> = RightJoinProps<RightJoinProps<ComponentProps, AsProps>, AdditionalProps> & {
    as?: AsComponent;
};
export declare type ComponentWithAs<Component extends As, Props extends object = {}> = {
    <AsComponent extends As = 'div'>(props: MergeWithAs<React.ComponentProps<Component>, React.ComponentProps<AsComponent>, Props, AsComponent>): JSX.Element;
    displayName?: string;
    propTypes?: React.WeakValidationMap<any>;
    contextTypes?: React.ValidationMap<any>;
    defaultProps?: Partial<any>;
    id?: string;
};

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 changes

Also, I agree this issue should be reopened

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found