0

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.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
pietro909
  • 1,811
  • 1
  • 19
  • 26

2 Answers2

2

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

pietro909
  • 1,811
  • 1
  • 19
  • 26
  • And how do I set this for eslint-plugin-prettier? Already have `parser: "@typescript-eslint/parser"` in my `.eslintrc`. – Piotr Ma'niak Aug 25 '22 at 12:59
0

pietro909's comment is correct, but if you're using .prettierrc for configuration, you can add this to the file, it would work similarly.

  "parser": "typescript"