Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning a type to a string that you are using as a react constructor #99

Closed
RowiDont opened this issue Mar 21, 2019 · 7 comments
Closed

Comments

@RowiDont
Copy link

RowiDont commented Mar 21, 2019

Hard to figure out how to write this, so I'll just give an example:

Example:

type Props = {
  headerTag: 'h1' | 'h2' | 'h3';
}

const ParentComponent: React.FC<Props> = (props) => {
  const HeaderTag = props.headerTag;
  const text = "Please someone help me type this correctly.";

  return <HeaderTag>{text}</HeaderTag>;
}

Error:

Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'.

It doesn't like me giving any children to <HeaderTag> even though I know <h1> etc. can obviously have string children.

I tried typing the HeaderTag variable but I didn't know what to type it as, because Typescript knows headerTag is just a string.

The solution I came up with isn't really what I want to do, since it uses React.createElement:

return React.createElement(props.headerTag, {}, text)

How should I be doing?

@ferdaber
Copy link
Collaborator

Hmm, this should work out of the box. Which version of TS and the react types are you on?

@RowiDont
Copy link
Author

Hmm, this should work out of the box. Which version of TS and the react types are you on?

typescript: 3.2.2
@types/react: 16.7.17

I can't check right now if an update will fix my problems, but will try that later today/tomorrow.

@RowiDont
Copy link
Author

I am now on:

typescript: 3.3.4000
@types/react: 16.8.8

I'm still getting this error.

Type '{ children: (string | Element)[]; className: any; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'className' does not exist on type 'IntrinsicAttributes'.

If I remove the className prop, I simply get:

Type '{ children: (string | Element)[]; }' has no properties in common with type 'IntrinsicAttributes'.

@ferdaber
Copy link
Collaborator

ferdaber commented Mar 22, 2019

With this entire codebase I get no errors with the same version:

import React from 'react'

type Props = {
  tag: 'h1' | 'h2' | 'h3'
}

export const Component = (props: Props) => {
  const text = 'abc'
  const HeaderTag = props.tag
  return <HeaderTag>{text}</HeaderTag>
}

Is there anything else you're leaving out? maybe a minimally-reproducible repo would help too.

@RowiDont
Copy link
Author

Okay, that's good to hear!

I'll see if I can make a small repo that has the issue.

@RowiDont
Copy link
Author

I cannot replicate it outside my project. Must be something about my current project that is causing this. Thanks for looking.

@swyxio
Copy link
Collaborator

swyxio commented Mar 27, 2019

ha, good to know. ifyou ever do figure out what blocked it, i'm super curious to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants