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

TSX variable element #8549

Closed
RylanH opened this issue May 10, 2016 · 4 comments
Closed

TSX variable element #8549

RylanH opened this issue May 10, 2016 · 4 comments
Labels
Bug A bug in TypeScript

Comments

@RylanH
Copy link

RylanH commented May 10, 2016

TypeScript Version:

1.8.10

Code

export class Text extends React.Component<TextProps, {}> {
  _tagName: string = 'div';

  render() {
    return (
      <this._tagName />
    );
  }
}

export class Heading1 extends Text {
 _tagName:string = 'h1';
}

Expected behavior:
No error

Actual behavior:
Throws compilation error 'Cannot find name this'

Workaround

export class Text extends React.Component<TextProps, {}> {
  _tagName: string = 'div';

  render() {
    const self = this;
    return (
      <self._tagName />
    );
  }
}

export class Heading1 extends Text {
 _tagName:string = 'h1';
}
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 10, 2016
@RyanCavanaugh RyanCavanaugh self-assigned this May 10, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.0 milestone May 10, 2016
@basarat
Copy link
Contributor

basarat commented May 11, 2016

Another:

const t = {tag:'h1'}
const foo = <t.tag/>

Error : JSX element t.tag does not have construct or call signatures.

I guess its valid to tag if the variable resolves to a string instead of a Call/Construct.

Warning

But not if its not dotted e.g. the following :

const tag ='h1';
const foo = <tag/>

is actually creating a React.createElement("tag") and not React.createElement("h1"). To be honest this whole use a string to point to what you want feels wrongish to me.

Type

Note : The type will probably have to resolve to any at least to begin with and it is going to be a pain 🌹

@mhegazy mhegazy assigned sandersn and unassigned RyanCavanaugh Jun 9, 2016
@yuit
Copy link
Contributor

yuit commented Jun 14, 2016

@sandersn could i take this bug ?

@sandersn
Copy link
Member

sandersn commented Jun 14, 2016

Sure, I haven't looked at it at all.

@sandersn sandersn assigned yuit and unassigned sandersn Jun 14, 2016
@yuit
Copy link
Contributor

yuit commented Jun 23, 2016

Update on the decision we made, after talk with @RyanCavanaugh about this. We made following decision:

  • For variable (including const) that has string type, it will be resolve to anyType (i.e we will not check if the string actually has valid Jsx intrinsic element type).
  • For variable with string literal type, we will check whether this string literal type is actually a valid Jsx intrinsic element type.

So in both examples pointed out by @RylanH and @basarat , the type will be anyType and no error should be reported. if you would like a tighter check, you will want to give it a string literal type

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants