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

import.meta.env类型造成歧义 #6930

Closed
7 tasks done
zhou-tao opened this issue Feb 15, 2022 · 5 comments
Closed
7 tasks done

import.meta.env类型造成歧义 #6930

zhou-tao opened this issue Feb 15, 2022 · 5 comments

Comments

@zhou-tao
Copy link

Describe the bug

尽管在 env.d.tsImportMetaEnv interface中 定义了环境变量 booblean 或者其他非 string 类型
在客户端使用import.meta.env仍得到一个string类型的值,但得到的 typescript 的类型提示确是自己定义过的

这可能会引起一个隐藏错误或歧义!

目前解决方案是所有 ImportMetaEnv 内属性均定义为 string , 自己手动转换为对应类型。这样不会导致直接使用 import.meta.env 得到一个与值不对应类型的类型提示。

我觉得这点需要在文档中标注出来!

Reproduction

System Info

System:
    OS: Windows 10 10.0.18363
    CPU: (16) x64 Intel(R) Xeon(R) CPU E5-2665 0 @ 2.40GHz
    Memory: 8.24 GB / 15.92 GB
  Binaries:
    Node: 14.17.4 - E:\software\node\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.14 - E:\software\node\npm.CMD
  Browsers:
    Edge: Spartan (44.18362.449.0)
    Internet Explorer: 11.0.18362.1
  npmPackages:
    @vitejs/plugin-legacy: ^1.6.4 => 1.6.4
    @vitejs/plugin-vue: ^2.0.0 => 2.1.0
    @vitejs/plugin-vue-jsx: ^1.3.3 => 1.3.3
    vite: 2.7.13 => 2.7.13

Used Package Manager

pnpm

Logs

No response

Validations

@scottopherson
Copy link

#6637

@bluwy
Copy link
Member

bluwy commented Apr 11, 2022

Closing as duplicate of #6637. Vite uses dotenv which parses .env files as string key-value pairs.

@bluwy bluwy closed this as completed Apr 11, 2022
@scottopherson
Copy link

@bluwy would it make sense to keep this topic in an issue instead of a discussion? ImportMetaEnv interface is incorrect and should be tracked as an issue imo.

@bluwy
Copy link
Member

bluwy commented Apr 12, 2022

There was a PR to fix it at #4415 but was closed due to #4415 (comment). It seems like it make sense to type is as any though

@skimhugo
Copy link

As far as I know, we can just obtain a boolean env var via define which is compatible with esbuild.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants