Skip to content

Unhelpful error with StatelessComponents #10170

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

Closed
alexjg opened this issue Aug 5, 2016 · 5 comments
Closed

Unhelpful error with StatelessComponents #10170

alexjg opened this issue Aug 5, 2016 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@alexjg
Copy link

alexjg commented Aug 5, 2016

TypeScript Version: 1.8.10

Code

File: src/App.tsx

/// <reference path="../typings/index.d.ts" />
import * as React from "react"
import Child from "./Child"

const App = () => {
    return (<div>
        <h1>Hello World</h1>
        <Child />
        <Child />
    </div>)
}

export default App

File: Child.tsx

/// <reference path="../typings/index.d.ts" />
import * as React from "react"

interface ChildProps {
    bold: boolean
}

const Child: React.StatelessComponent<ChildProps> = ({bold}) => {
    if (bold) {
        return <div style={{fontWeight: "700"}}>Child</div>
    }
    return <div>Child</div>
}

export default Child

File: tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "jsx": "preserve"
    }
}

Expected behavior:
An error free compile

Actual behavior:

/usr/local/lib/node_modules/typescript/lib/tsc.js:33078
                throw e;
                ^

TypeError: Cannot read property 'flags' of undefined
    at isRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:16013:32)
    at checkTypeRelatedTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:15941:26)
    at checkTypeAssignableTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:15819:20)
    at isTypeAssignableTo (/usr/local/lib/node_modules/typescript/lib/tsc.js:15813:20)
    at getJsxElementAttributesType (/usr/local/lib/node_modules/typescript/lib/tsc.js:18463:47)
    at checkJsxOpeningLikeElement (/usr/local/lib/node_modules/typescript/lib/tsc.js:18564:40)
    at checkJsxSelfClosingElement (/usr/local/lib/node_modules/typescript/lib/tsc.js:18265:13)
    at checkJsxElement (/usr/local/lib/node_modules/typescript/lib/tsc.js:18281:25)
    at checkExpressionWorker (/usr/local/lib/node_modules/typescript/lib/tsc.js:20437:28)
    at checkExpression (/usr/local/lib/node_modules/typescript/lib/tsc.js:20349:42)

I imagine I'm doing something wrong here but I can't figure out what it is because the error message is a little obtuse.

There's a repo demoing the whole problem here: https://github.com/alexjg/typescript-error-demo

Thanks

@basarat
Copy link
Contributor

basarat commented Aug 5, 2016

"React.StatelessComponent" aren't you supposed to inherit from it? I didn't take a look now but that is what a gathered from my last read of the release notes 🌹

@alexjg
Copy link
Author

alexjg commented Aug 5, 2016

Ah, maybe I've misunderstood the way it's mean to be used, I was going based on this blog post and the definition of the StatelessComponent, which looks like this:

interface StatelessComponent<P> {
    (props?: P, context?: any): ReactElement<any>;
    propTypes?: ValidationMap<P>;
    contextTypes?: ValidationMap<any>;
    defaultProps?: P;
    displayName?: string;
}

Which looks to me like it's a function which returns a ReactElement but has some extra properties right? I'm new to TypeScript so very likely to be reading it wrong.

@kitsonk
Copy link
Contributor

kitsonk commented Aug 5, 2016

Irrespective of the potential "misuse" you have encountered a tsc compiler error, which shouldn't happen, versus a legitimate TypeScript language error. I am sure the TypeScript team will be along soon to triage it and figure what is going wrong with the compiler.

@basarat
Copy link
Contributor

basarat commented Aug 5, 2016

My misunderstanding. The new thing I was thinking is a new "class" called React.PureComponent 🌹

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Aug 5, 2016
@RyanCavanaugh
Copy link
Member

Looks like a duplicate of #7286, which has been fixed in 2.0

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants