1

Using typescript to define a test outputs Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead. error.

./app.test.ts

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div); // here the error is produced
});

How should the component be defined in Typescript?

Example code from CRA Facebook page


edit: Accepted answer by @rzelek pointed in the right direction.

As defined in the JSX handbook the compiler options define the way your JSX is interpreted. In my case used "jsx": "react" so that means the method React.createElement() needs to be used in order to create a component (see table in JSX handbook):

final result: ./app.test.ts

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const app = React.createElement(App);
  const div = document.createElement('div');
  ReactDOM.render(app, div);
});
2
  • Is this in a tsx file ? Commented Jun 18, 2019 at 7:57
  • Yes, also tried it as ts. Commented Jun 18, 2019 at 8:13

2 Answers 2

1

Typescript files that contain JSX should have *.tsx extension instead of .ts.

You can see similar issue here: https://github.com/palantir/tslint-react/issues/141

Additionally, your tsconfig.json should have appropriate config. For my CRA apps, this is:

{
  "compilerOptions": {
    ...
    "jsx": "preserve",
    ...
  }
}

https://www.typescriptlang.org/docs/handbook/jsx.html

The error you can see most probably comes from tslint, not Typescript itself. You can see a rule here: https://palantir.github.io/tslint/rules/no-angle-bracket-type-assertion/. Basic tslint config for CRA should be something similar to this:

{
  "extends": ["tslint:latest", "tslint-react"],
  "rules": {
    // override tslint-react rules here
    "jsx-wrap-multiline": false
  }
}

As a good starting point, you can try adding "no-angle-bracket-type-assertion": false to tslint.json rules and see if error disappears.

PS. tslint usually points you to source of the error, for example: ERROR: app/components/Root/index.ts[6, 7]: Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.

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

2 Comments

Yes, the app has it: App.tsx. Also tried app.test.tsx and app.test.ts
Tried "jsx": "preserve", and "jsx": "react",
0

How's your <App> component defined?

Let's assume these are the component props and state interfaces (I usually define them in the beginning of a file for each component):

interface Props {
   //... some props
}
interface State {
   //... some state
}

A class component can be defined like this

class MyComponent extends React.Component<Props, State> { ... }

A function component can be defined like this

const MyComponent: React.FC<Props> = ...

You might want to check react-redux-typescript-guide

Also, this question might be related

2 Comments

That is correct, defined it as: class App extends React.Component<IAppProps, IAppState>
Do you use type assertion in your component? see: stackoverflow.com/questions/44202311/…

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.