Search code examples
typescriptprettierreact-tsx

Prettier doesn't parse Typescript type cast


I'm using Prettier 1.17.1 and TypeScript 3.4.5 with create-react-app.

I'm getting the error below each time I try to use the x as T syntax in TypeScript for type cast:

src/Form.tsx
[error] src/Form.tsx: SyntaxError: Unexpected identifier (82:57)
[error]   80 |                 value={data.position}
[error]   81 |                 onChange={e => {
[error] > 82 |                     const nextPosition = e.target.value as Position

I've tried also a different syntax <T> x but it breaks TS compilation due to the JSX format.


Solution

  • Solved it! I had to explicitly set the --parser option when running it. In the docs, it says that it should be inferred, though it wasn't working for me with .tsx files.

    prettier --parser typescript fixed it