9

I am trying to create and compile an React Higer Order Component in Typescript.

But I get an error. Firstly VSCode complains about this

'WrappedComponent' refers to a value, but is being used as a type here.

** And my Typescript compiler throws this error.**

lib/auth.ts:44:30 - error TS1005: '>' expected.

44     return <WrappedComponent {...this.props} />;
                                ~

lib/auth.ts:44:47 - error TS1109: Expression expected.

44     return <WrappedComponent {...this.props} />;
                                                 ~

lib/auth.ts:44:48 - error TS1109: Expression expected.

44     return <WrappedComponent {...this.props} />;

**Here is my code below of the React HOC component **

import * as React from 'react';
import Router from 'next/router';
import { NextContext } from 'next';
import nextCookie from 'next-cookies';
import { MOVED_TEMPORARILY } from 'http-status-codes';

interface ComponentExtra extends React.Component, React.SFC {
  getInitialProps: Function;
}

export const withAuthSync = (WrappedComponent: ComponentExtra) => class extends React.Component {
  static async getInitialProps(ctx: NextContext) {
    const token = auth(ctx);
    const componentProps = WrappedComponent.getInitialProps && (await WrappedComponent.getInitialProps(ctx));
    return { ...componentProps, token };
  }

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

export const auth = (ctx: NextContext) => {
  const { req, res } = ctx;
  const { token } = nextCookie(ctx);

  if (req && res && !token) {
    res.writeHead(MOVED_TEMPORARILY, { Location: '/signin' }).end();
    return;
  }

  return token;
};

**Update the error was due to the file extension was not .tsx but .ts **

7
  • I forgot to mention that the error occurs in the render function return line Commented Apr 14, 2019 at 20:38
  • What does the error say? edit your question and add the error log and line number of the error, and any compile time error in the editor, if there is any TSLint setup Commented Apr 14, 2019 at 20:51
  • The question should contain stackoverflow.com/help/mcve . This piece of code doesn't use WrappedComponent as a type and can't cause this error. Commented Apr 15, 2019 at 6:04
  • 4
    It isn't parsed as JSX syntax, that's the problem. It should be auth.tsx and not auth.ts, for starters. In case TS was configured correctly, that's the only fix that is needed. Commented Apr 15, 2019 at 9:50
  • Omg that was the problem. Thank you very much @estus Commented Apr 15, 2019 at 12:37

2 Answers 2

39

The error means that JSX syntax isn't parsed as such:

lib/auth.ts:44:30 - error TS1005: '>' expected.

44     return <WrappedComponent {...this.props} />;

In order to do that, auth.ts should be renamed to auth.tsx. In case TypeScript was configured correctly for JSX, this the only fix that is need.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks in my case i had it .ts instead of .tsx
0

I had the same error, but for a different reason. I had a commented out a JSX element in the previous line, as such:

file1.tsx
  export const WrappedComponent = HocFunction(Component);


file2.tsx
  import { WrappedComponent }  from './file1.tsx';
  ...
  // <SomeJSXTag />
  <WrappedComponent /> --- Error: 'WrappedComponent' refers to a value, but is being used as a type here.

I finally fixed it by changing the comment to look like this:

  {/*
  <SomeJSXTag />
  */}
  <WrappedComponent /> --- Error Disappeared

and the error went away.

For now, this might just be JSX behavior: https://wesbos.com/react-jsx-comments/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.