Skip to content

Default linter and/or linting configuration cannot parse various TypeScript features (e.g. modifiers like readonly) #6420

Open

Description

@AlphaHydrae

Is this a bug report?

Yes

Did you try recovering your dependencies?

The issue occurs on a freshly created application.

Which terms did you search for in User Guide?

Linting, Readonly, TypeScript

Environment

Environment Info:

  System:
    OS: macOS Sierra 10.12.6
    CPU: x64 Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
  Binaries:
    Node: 10.15.1 - ~/.nodenv/versions/10.15.1/bin/node
    npm: 6.7.0 - ~/.nodenv/versions/10.15.1/bin/npm
  Browsers:
    Chrome: 72.0.3626.96
    Firefox: 64.0.2
    Safari: 12.0.3
  npmPackages:
    react: ^16.8.1 => 16.8.1 
    react-dom: ^16.8.1 => 16.8.1 
    react-scripts: 2.1.5 => 2.1.5 
  npmGlobalPackages:
    create-react-app: 2.1.5

Steps to Reproduce

  1. Generate an app with the typescript option:

    create-react-app my-app --typescript
    
  2. Add TypeScript code with the readonly modifier, for example in src/App.tsx:

    interface Test {
      readonly foo: string
    }
    
  3. Observe that eslint cannot parse the code, as indicated by the editor if integrated with eslint, or by running the following command:

    $> ./node_modules/eslint/bin/eslint.js src/App.tsx
    
    /path/to/my-app/src/App.tsx
      6:12  error  Parsing error: Unexpected token, expected ":"
    
      4 | 
      5 | interface Test {
    > 6 |   readonly foo: string;
        |            ^
      7 | }
      8 | 
      9 | class App extends Component {
    
    ✖ 1 problem (1 error, 0 warnings)
    

Expected Behavior

The linter with its default configuration should be able to parse this interface.

Actual Behavior

The linter fails to parse the interface with the error:

error  Parsing error: Unexpected token, expected ":"

Removing the readonly modified removes the parsing error.

Reproducible Demo

This repository demonstrates the issue with a simple commit.

Run the following command to reproduce:

git clone https://github.com/AlphaHydrae/create-react-app-typescript-readonly-bug.git && \
  cd create-react-app-typescript-readonly-bug && \
  npm ci && \
  ./node_modules/eslint/bin/eslint.js src/App.tsx

Activity

  1. AlphaHydrae commented on Feb 13, 2019

    @AlphaHydrae
    Author

    I just stumbled over another similar problem. The following valid TypeScript also causes the linter's parsing to fail:

    function foo<T = any>() {
    }
    

    Although this works fine:

    interface Foo<T = any> {
    }
    
  2. changed the title [-]Default linter and/or linting configuration cannot parse TypeScript's readonly modifier[/-] [+]Default linter and/or linting configuration cannot parse TypeScript's modifiers (e.g. readonly)[/+] on Feb 13, 2019
  3. Nimelrian commented on Feb 15, 2019

    @Nimelrian

    eslint also trips over this:

    type Id<T> = {
        [Prop in keyof T]: T[Prop];
    }
    Parsing error: Unexpected token, expected "]"
    
       6 | 
       7 | type Id<T> = {
    >  8 |     [Prop in keyof T]: T[Prop];
         |           ^
       9 | }
      10 | 
      11 | type S = Id<{foo: "bar"}>;
    

    It seems like the babel-eslint parser configured in the config doesn't handle TypeScript. Here's the config from the eslint-config-react-app package:
    https://github.com/facebook/create-react-app/blob/319cf9b9e1833a4b31209b9dea0d03e89b996d71/packages/eslint-config-react-app/index.js#L29
    https://github.com/facebook/create-react-app/blob/319cf9b9e1833a4b31209b9dea0d03e89b996d71/packages/eslint-config-react-app/index.js#L41-L47

  4. changed the title [-]Default linter and/or linting configuration cannot parse TypeScript's modifiers (e.g. readonly)[/-] [+]Default linter and/or linting configuration cannot parse various TypeScript features (e.g. modifiers like readonly)[/+] on Feb 15, 2019
  5. sbusch commented on Feb 22, 2019

    @sbusch

    I have a similar (the same?) problem, see #6360. Tested with CRA 2.1.5 and current HEAD.

  6. nsmithdev commented on Mar 11, 2019

    @nsmithdev

    Add TypeScript linting support
    Issue #5641
    Pull #6513

  7. ianschmitz commented on Mar 11, 2019

    @ianschmitz
    Contributor

    ESLint is not currently running on .ts or .tsx files. Any errors you're seeing are from the TypeScript compiler type checking. If you're seeing errors today it's likely because it's not valid TypeScript.

  8. Nimelrian commented on Mar 11, 2019

    @Nimelrian

    That's strange, because simply creating a new project with yarn create react-app test --typescript, adding an .eslintrc.json to the project root containing only { "extends": "react-app" } and using the ESLint plugin for VS Code shows this error for perfectly valid TS code:
    image

  9. JustinTRoss commented on Mar 31, 2019

    @JustinTRoss

    Same for keyof.

    image

  10. ianschmitz commented on Apr 1, 2019

    @ianschmitz
    Contributor

    Interesting! Here's our ESLint setup in webpack as of 2.1.8: https://github.com/facebook/create-react-app/blob/c17e25bf7f9dd60c49cd491535114beb5592ff81/packages/react-scripts/config/webpack.config.js#L307.

    Not sure how it would be running on typescript files. May be worth trying our 3.0 alpha to see if it resolves your issue as it includes typescript ESLint support.

  11. JustinTRoss commented on Apr 1, 2019

    @JustinTRoss

    Wow. That is weird. I was able to resolve the issues by defining my own eslint config with typescript parser and plugin.

  12. foffer commented on Jan 1, 2021

    @foffer

    Is there any update on this, I'm seeing the same issue using react-scripts 4.0.1

    Screenshot 2021-01-01 at 08 47 38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions