在我的应用程序(基于 create-react-app 的 React)的源代码中,我使用了这样的 env 变量:process.env.REACT_APP_API_URL它们存储在我的.env.*文件中。
但是当我在 Cypress 下运行相同的应用程序时,process.env对象是空的。当 React 应用程序在 Cypress 下运行时,如何提供这些变量以在 React 应用程序中使用?
我知道我可以设置 Cypress env 变量,但这不是我想要的 - 这是一个不同的范围。
我收到TS错误:
TypeScript错误:类型'undefined'不能用作索引类型。TS2538
对于此简单功能(根据提供的索引从数组中获取对象):
const myArr: Array<object> = [{name: 'John'}, {name: 'Tom'}]
function getData(index?: number) {
const isIndex : boolean = typeof index !== 'undefined';
return isIndex ? myArr[index] : {};
}
Run Code Online (Sandbox Code Playgroud)
对我来说,更神秘的是,当我将其更改为:
function getData(index?: number) {
return typeof index !== 'undefined' ? myArr[index] : {};
}
Run Code Online (Sandbox Code Playgroud)
一切都像魅力一样-为什么?