Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

taro下typescript无法检测错误? #4348

Closed
BUPTlhuanyu opened this issue Aug 30, 2019 · 4 comments
Closed

taro下typescript无法检测错误? #4348

BUPTlhuanyu opened this issue Aug 30, 2019 · 4 comments
Labels
question Further information is requested

Comments

@BUPTlhuanyu
Copy link

BUPTlhuanyu commented Aug 30, 2019

问题描述
基于typescript的taro官方示例中,interface state中与组件中的state的数据不一致,编译也通过了,并且不会进行预编译报错提示。

复现步骤
[复现问题的步骤]

  1. 在目录下taro init taro-t1创建项目
  2. 提示是否需要使用typescript的时候,选择是
  3. 来到 taro-t1目录下,修改taro-t1\src\pages\index\index.tsx如下:
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './index.scss'

interface State{
  // list: any[]
}

interface Props{}

export default class Index extends Component<Props, State> {

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

  state = {
    list: [
      {selected: true,text: 'one'},
      {selected: true,text: 'two'},
      {selected: true,text: 'three'}
    ]
  }

  componentWillMount () { }

  componentDidMount () { }

  componentWillUnmount () { }

  componentDidShow () { }

  componentDidHide () { }

  render () {
    const list = this.state.list
        .filter(l => l.selected)
        .map(l => {
          return <li key={l.text}>{l.text}</li>
        })
    return (
      <View className='index'>
        {list}
        <Text>Hello world!</Text>
      </View>
    )
  }
}

  1. npm run dev:h5编译通过,貌似这里的typescript是个摆设?

期望行为
typescript检查出错,编译不通过

报错信息

无,编译成功

系统信息
👽 Taro v1.3.15

Taro CLI 1.3.15 environment info:
System:
OS: Windows 10
Binaries:
Node: 10.13.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.16.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD

补充信息

@taro-bot
Copy link

taro-bot bot commented Aug 30, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@yuche
Copy link
Contributor

yuche commented Aug 30, 2019

编译器是不会检查类型错误的,如果你需要检查可以自行加一个 tsc 命令在 npm run build 之前。

@yuche yuche added answered question Further information is requested labels Aug 30, 2019
@taro-bot
Copy link

taro-bot bot commented Aug 30, 2019

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

@BUPTlhuanyu
Copy link
Author

您好,tsc也不管用,在类型有错误的时候,依旧可以编译通过,是否可以效仿create-react-app加上ts-pnp,fork-ts-webpack-plugin在ts类型出错的时候终端项目的编译?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants