taro icon indicating copy to clipboard operation
taro copied to clipboard

taro+typescript+mobx 初始化项目 taro doctor 后出现 54:22 error Parsing error: Unexpected token, expected ";"

Open yikalosi opened this issue 7 years ago • 7 comments

版本1.3.0-beta.8 init 一个demo taro+typescript+mobx 问题 taro doctor后出现

54:22 error Parsing error: Unexpected token, expected ";"

52 | } 53 |

54 | export default Index as ComponentType | ^ 55 |

✖ 1 problem (1 error, 0 warnings)

yikalosi avatar Jun 14 '19 03:06 yikalosi

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

taro-bot avatar Jun 14 '19 03:06 taro-bot

贴一下报错页面的完整代码

luckyadam avatar Jun 14 '19 03:06 luckyadam

import { ComponentType } from 'react'
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components'
import { observer, inject } from '@tarojs/mobx'

import './index.scss'

type PageStateProps = {
  counterStore: {
    counter: number,
    increment: Function,
    decrement: Function,
    incrementAsync: Function
  }
}

interface Index {
  props: PageStateProps;
}

@inject('counterStore')
@observer
class Index extends Component {

  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  config: Config = {
    navigationBarTitleText: '首页'
  }

  componentWillMount () { }

  componentWillReact () {
    console.log('componentWillReact')
  }

  componentDidMount () { }

  componentWillUnmount () { }

  componentDidShow () { }

  componentDidHide () { }

  increment = () => {
    const { counterStore } = this.props
    counterStore.increment()
  }

  decrement = () => {
    const { counterStore } = this.props
    counterStore.decrement()
  }

  incrementAsync = () => {
    const { counterStore } = this.props
    counterStore.incrementAsync()
  }

  render () {
    const { counterStore: { counter } } = this.props
    return (
      <View className='index'>
        <Button onClick={this.increment}>+</Button>
        <Button onClick={this.decrement}>-</Button>
        <Button onClick={this.incrementAsync}>Add Async</Button>
        <Text>{counter}</Text>
      </View>
    )
  }
}

export default Index  as ComponentType

yikalosi avatar Jun 14 '19 09:06 yikalosi

下面两种写法taro doctor也是提示Parsing error: Unexpected token

interface Itest {
  id: string
  name: string
}
let someValue: any = 'this is a string'
let strLength: number = (someValue as string).length

liu-dongyu avatar Jun 21 '19 07:06 liu-dongyu

+1 碰到相同的问题,是babel-eslint的问题~

hnsylitao avatar Jul 02 '19 02:07 hnsylitao

请问这个问题解决了吗?

benny-eu avatar Jul 18 '19 04:07 benny-eu

临时解决方案。

  1. 安装开发依赖
yarn add @typescript-eslint/eslint-plugin --dev
  1. 编辑 .eslintrc 文件,添加 overrides 项,代码内容来自 https://github.com/facebook/create-react-app/blob/master/packages/eslint-config-react-app/index.js#L55

完整的 .eslintrc,rules 可以自行调整

module.exports = {
  extends: ["taro"],
  parser: "babel-eslint",
  rules: {
    "no-unused-vars": ["error", { "varsIgnorePattern": "Taro" }],
    "react/jsx-filename-extension": [
      1,
      { "extensions": [".js", ".jsx", ".tsx"] }
    ]
  },
  overrides: [
    {
      files: ['**/*.ts?(x)'],
      parser: '@typescript-eslint/parser',
      parserOptions: {
        ecmaVersion: 2018,
        sourceType: 'module',
        ecmaFeatures: {
          jsx: true,
        },

        // typescript-eslint specific options
        warnOnUnsupportedTypeScriptVersion: true,
      },
      plugins: ['@typescript-eslint'],
      // If adding a typescript-eslint version of an existing ESLint rule,
      // make sure to disable the ESLint rule here.
      rules: {
        // TypeScript's `noFallthroughCasesInSwitch` option is more robust (#6906)
        'default-case': 'off',
        // 'tsc' already handles this (https://github.com/typescript-eslint/typescript-eslint/issues/291)
        'no-dupe-class-members': 'off',
        // 'tsc' already handles this (https://github.com/typescript-eslint/typescript-eslint/issues/477)
        'no-undef': 'off',

        // Add TypeScript specific rules (and turn off ESLint equivalents)
        '@typescript-eslint/no-angle-bracket-type-assertion': 'warn',
        'no-array-constructor': 'off',
        '@typescript-eslint/no-array-constructor': 'warn',
        '@typescript-eslint/no-namespace': 'error',
        'no-use-before-define': 'off',
        '@typescript-eslint/no-use-before-define': [
          'warn',
          {
            functions: false,
            classes: false,
            variables: false,
            typedefs: false,
          },
        ],
        'no-unused-vars': 'off',
        '@typescript-eslint/no-unused-vars': [
          'warn',
          {
            args: 'none',
            ignoreRestSiblings: true,
          },
        ],
        'no-useless-constructor': 'off',
        '@typescript-eslint/no-useless-constructor': 'warn',
      },
    },
  ],
};

fishead avatar Jul 19 '19 10:07 fishead