Taro: cli使用ts生成的模板报错

Created on 27 Dec 2019  ·  13Comments  ·  Source: NervJS/taro

问题描述

使用taro init 生成ts工程检查报错

复现步骤

  1. taro init testApp
  2. 使用typescript
  3. 使用sass
  4. 使用默认模板
  5. 使用vscode打开App.tsx
/**
 * 这段注释后可以贴代码
 * 提供完整可复现的代码和整理好代码格式,有助于我们快速定位问题,节省你我时间
 * 代码提供不全或代码格式混乱的 issues 【有可能会被忽略】
 * 
 * 查看如何插入代码:https://coding.net/help/doc/project/markdown.html#i-5
 */

import Taro, { Component, Config } from '@tarojs/taro'
import Index from './pages/index'

import './app.scss'

// 如果需要在 h5 环境中开启 React Devtools
// 取消以下注释:
// if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5')  {
//   require('nerv-devtools')
// }
/*  */
class App extends Component {

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

  componentDidMount () {}

  componentDidShow () {}

  componentDidHide () {}

  componentDidCatchError () {}


  config: Config = {
    pages: [
      'pages/index/index'
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black'
    }
  }
  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Index />
    )
  }
}

Taro.render(<App />, document.getElementById('app'))

期望行为

控制台无问题输出

报错信息


'Config' is defined but never used. Allowed unused vars must match /Taro/.

系统信息


Taro CLI 1.3.25 environment info:
System:
OS: Windows 10
Binaries:
Node: 12.13.0 - D:\nodejs\node.EXE
Yarn: 1.19.1 - C:\Users\Herbert\AppData\Roaming\npm\yarn.CMD
npm: 6.12.0 - D:\nodejs\npm.CMD

补充信息

刚生成的工程文件中Config的位置不对警告,可以改到生命周期的下面解决

如果您有功能上的建议,可以提到 FeatHub

使用上的问题,欢迎在「Taro 社区」一起交流

bug

All 13 comments

欢迎提交 Issue~

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

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

Good luck and happy coding~

报错应该不是这个文件吧。
而且这是个 ESLint 问题,如果觉得烦可以修改 .eslintrc

报错应该不是这个文件吧。
而且这是个 ESLint 问题,如果觉得烦可以修改 .eslintrc

// eslint-disable-next-line no-unused-vars 直接使用这个也可以吗

可以的。

Taro CLI 1.3.25 可以升级啦。升级到最新版本。1.3.25是有一些小问题的。 @HerbertHe

CC @luckyadam

同样的问题。 我个人是在 .eslintrc 里边直接 "no-unused-vars":0 全禁止了,一了百了。。
taro版本为1.3.29。

@shenghanqin 升级1.3.29这个问题还是存在

:octocat: From gitme Android

好像是 eslint@typescript-eslint/parser 之间出了点问题,安装一下 @typescript-eslint/eslint-plugin 然后在配置里添加一下插件就可以了 "extends": ["taro", "plugin:@typescript-eslint/recommended"],

大概是解决了,晚些时候同步到其他类型的模版里。

image

'Config' is defined but never used. Allowed unused vars must match /Taro/

@Garfield550 [taro-v2.0.1] 也存在同样的问题,手动添加 eslint 插件和配置后恢复正常

好像是 eslint@typescript-eslint/parser 之间出了点问题,安装一下 @typescript-eslint/eslint-plugin 然后在配置里添加一下插件就可以了 "extends": ["taro", "plugin:@typescript-eslint/recommended"],

按照文中方式 还是不管用

  2:8   warning  'Taro' is defined but never used                @typescript-eslint/no-unused-vars

� Taro v2.0.4


  Taro CLI 2.0.4 environment info:
    System:
      OS: Windows 10
    Binaries:
      Node: 10.16.3 - D:\Program Files\nodejs\node.EXE
      Yarn: E:\code\challenge>"node"  "C:\Users\Administrator\AppData\Roaming\npm\\node_modules\yarn\bin\yarn.js" -v
1.19.1 - C:\Users\Administrator\AppData\Roaming\npm\yarn.CMD
      npm: 6.9.0 - D:\Program Files\nodejs\npm.CMD

配置文件

{
  "extends": [
    "taro",
    "plugin:@typescript-eslint/recommended"
  ],
  "parser": "@typescript-eslint/parser",
  "rules": {
    "no-unused-vars": ["error", { "varsIgnorePattern": "Taro" }],
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [
          ".js",
          ".jsx",
          ".tsx"
        ]
      }
    ],
    "@typescript-eslint/explicit-function-return-type": 0,
    "@typescript-eslint/no-empty-function": [
      "error"
    ]
  },

  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "useJSXTextNode": true,
    "project": "./tsconfig.json"
  }
}

================更新==================
更改后解决问题

-  "no-unused-vars": ["error", { "varsIgnorePattern": "Taro" }]
+  "@typescript-eslint/no-unused-vars": ["error", { "varsIgnorePattern": "Taro" }]

改下配置文件就行

"no-unused-vars": ["error", { "varsIgnorePattern": "[Taro|Config]" }]
Was this page helpful?
0 / 5 - 0 ratings