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

defineConstants在webstorm报错 #5220

Closed
LaamGinghong opened this issue Dec 30, 2019 · 5 comments
Closed

defineConstants在webstorm报错 #5220

LaamGinghong opened this issue Dec 30, 2019 · 5 comments

Comments

@LaamGinghong
Copy link

问题描述

在config中定义了defineConstants,使用没有问题,但是ide会报错

代码

//  dev.js
module.exports = {
  env: {
    NODE_ENV: '"development"'
  },
  defineConstants: {
    BASE_URL: '"http://station.env-zside.dev.k8s.guanmai.cn"'
  },
  mini: {},
  h5: {}
}
const url = BASE_URL
// ide 提示TS2304: Cannot find name 'BASE_URL'.

期望行为

不会报错,目前使用@ts-ignore

报错信息

TS2304: Cannot find name 'BASE_URL'.

系统信息

System:
OS: macOS 10.15.2
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.13.0 - ~/.nvm/versions/node/v12.13.0/bin/node
Yarn: 1.21.1 - ~/.yarn/bin/yarn
npm: 6.12.0 - ~/.nvm/versions/node/v12.13.0/bin/npm
npmPackages:
@tarojs/components: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/components-qa: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/mini-runner: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/mobx: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/mobx-h5: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/router: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro-alipay: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro-h5: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro-qq: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro-quickapp: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro-swan: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro-tt: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/taro-weapp: 2.0.0-beta.10 => 2.0.0-beta.10
@tarojs/webpack-runner: 2.0.0-beta.10 => 2.0.0-beta.10
eslint-config-taro: 2.0.0-beta.10 => 2.0.0-beta.10
eslint-plugin-taro: 2.0.0-beta.10 => 2.0.0-beta.10
nerv-devtools: ^1.5.6 => 1.5.6
nervjs: ^1.5.6 => 1.5.6
stylelint-config-taro-rn: 2.0.0-beta.10 => 2.0.0-beta.10
stylelint-taro-rn: 2.0.0-beta.10 => 2.0.0-beta.10

补充信息

// tsconfig.json
{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "removeComments": false,
    "preserveConstEnums": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "noImplicitAny": false,
    "allowSyntheticDefaultImports": true,
    "outDir": "lib",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "baseUrl": ".",
    "rootDir": ".",
    "jsx": "preserve",
    "jsxFactory": "Taro.createElement",
    "allowJs": true,
    "resolveJsonModule": true,
    "typeRoots": [
      "node_modules/@types",
      "global.d.ts"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ],
  "include": [
    "**/*.tsx",
    "**/*.ts"
  ],
  "compileOnSave": false
}
@taro-bot
Copy link

taro-bot bot commented Dec 30, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@Garfield550
Copy link
Collaborator

Garfield550 commented Dec 30, 2019

defineConstants 一定会在 TS 里报错,所以请在 global.d.ts 里手动添加类型定义 declare const BASE_URL: string;

@shen-lan
Copy link

shen-lan commented Feb 5, 2021

BASE_URL

@Garfield550 global.d.ts 里添加了声明 vscode依然提示 ts找不到变量

  global.d.ts 
---------------
declare const basicToken: string
any
找不到名称“basicToken”。ts(2304)

@SimbaOvO
Copy link

我写了个Taro Plugin来兼容defineConstants需要手动声明 不然编辑器报错的问题 大家可以试试~

@lihe6666
Copy link

defineConstants 一定会在 TS 里报错,所以请在 global.d.ts 里手动添加类型定义 declare const BASE_URL: string;

正解

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

No branches or pull requests

5 participants