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.

Dynamic TagName causing error after version upgrade

See original GitHub issue

typescript version: 3.2.2 IDE: VS Code

Description: I updated from v3.1.6 to v3.2.2. This is my code:

import * as React from 'react';

export interface Props {
  tag?: string,
};

const MyComponent: React.FunctionComponent<Props> = ({
  tag: Tag = 'div', children
}) => {
  return (
    <Tag> // this is causing the error
      {children}
    </Tag>
  );
};

Expected: No error

Actual: Error produced: [ts] Type '{ children: ReactNode; }' has no properties in common with type 'IntrinsicAttributes'. [2559]

This seems like a regression as it worked in v3.1.6

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
weswighamcommented, Feb 22, 2019

For anyone reading this: This error is pretty much correct and is because making a tag out of an arbitrary string isn’t really typesafe. The NeedsInvestigation tag is, IIRC, just to find why it worked before we refactored how we handled JSX (it shouldn’t have from what I know…). In your code, if you want to say that you have a valid html tag name, you should use keyof React.ReactDOM instead of string (which, FYI contains both the HTML and SVG elements - if you only intend for one or the other, you should select either ReactSVG or ReactHTML instead). JSX.IntrinsicElements contains no definition for the props for something of type string (that would require an index signature, which react does not define). This makes perfect sense - react has no idea how to make a "foobar" tag correctly. Within react’s types, ReactType is defined in a similar fashion (in terms of JSX.IntrinsicElements).

If anything, I’d say that the preferred workaround for anyone who finds this, rather than suppressing the error, is trying to rejigger their string type into the keyof React.ReactDOM (or keyof JSX.IntrinsicElements) union instead.

4reactions
zpostencommented, Nov 26, 2019

This seems to work:

export function DynamicTag(props: DynamicTagProps) {
  let {tag: Tag, ...rest} = props

  return <Tag {...rest} />
}

export type DynamicTagProps = React.HTMLAttributes<HTMLOrSVGElement> & {
  children: string
  tag?: keyof JSX.IntrinsicElements
}

DynamicTag.defaultProps = {tag: 'div'}

This is the calculated type of tag:

image

…until you try to add a ref prop to Tag. Then you get the error:

Expression produces a union type that is too complex to represent. ts(2590)
Read more comments on GitHub >

github_iconTop Results From Across the Web

When are dynamic tags applied? - Qualys Discussions
We wanted to create a dynamic tag for the presence of a specific QID. This seems like a pretty straightforward process. Create New...
Read more >
Cannot change version of project facet Dynamic Web Module ...
Change the dynamic web module version in this line to 3.0 - <installed ... Right Click on The Project Folder > Maven >...
Read more >
AdWords Conversion Tracking Errors - Tag Assistant Help
This means that Conversion Tracking code is not implemented properly on the page and no conversions will be tracked. Solution: Move the Conversion...
Read more >
What's Wrong With The Docker :latest Tag? - vsupalov.com
Docker images tagged with :latest have caused many people a lot of trouble. ... Tell immediately what code version is running based on...
Read more >
Dynamic policies - FortiClient EMS
EMS dynamically updates these endpoint groups when host compliance or other events occur, causing FortiOS to dynamically adjust its security policies based on...
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