typescript - TS2339:“Cypress 和 EventEmitter”类型上不存在属性“dayjs”
问题描述
我最近重构了我的代码以使用 dayJS 而不是 Moment.js,现在 Webstorm 报告了大量的 TS2339 错误。这些错误不会阻止我的代码编译或运行,但它使查找实际错误变得非常困难。这是显示错误的一些代码示例:
export function verifyCreatedDate(date: string) {
cy.log('Verify item creation date');
cy.get('[data-cy="timeline-date"]').should(
'have.text',
Cypress.dayjs(date).format('MM/DD/YY'),
);
}
这是我的tsconfig.json
文件:
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": false,
"module": "es2015",
"target": "es2015",
"jsx": "react",
"allowJs": true,
"types": ["cypress", "@percy/cypress"],
"moduleResolution": "node",
"typeRoots": ["node_modules/@types"],
"lib": ["es2018", "dom"]
},
"include": ["**/*.ts", "node_modules/cypress/types/mocha/index.d.ts"]
}
我尝试将 dayJS 添加到"types"
和"include"
部分,然后重新启动 WebStorm,但这也没有解决错误。我一直在寻找这个问题的答案大约一周,虽然这是一个常见问题,但我发现的建议解决方案都没有奏效。
support/index.js
最后,这是我文件的相关部分:
// if multiple specs need to use dayjs import it in the support file
// and add to the global Cypress object
const dayjs = require('dayjs');
Cypress.dayjs = dayjs;
解决方案
cypress/support/index.d.ts
:
/// <reference types="cypress" />
declare namespace Cypress {
interface Chainable {
... your custom commands
}
import dayjs from 'dayjs';
interface Cypress {
dayjs: dayjs.Dayjs;
}
}
推荐阅读
- c++ - 从 C++ 文件中搜索艺术家姓名和歌曲
- javascript - 基于属性组合对象数组内部数组
- python - 如果字符串不是字母数字,则无法阻止函数工作
- python - 如何制作由(例如)3 的倍数组成的 Python 数组?
- python - 在神经网络中,如何通过矩阵乘法计算梯度?为什么?
- python - 模块 'pandas' 没有 'read_csv' 成员 pylint(no-member)
- python-3.x - 由于路径问题,无法将 python 项目变成可执行文件?
- javascript - M 寻找可以根据数据库中可用数据在图像上显示热点的方法
- python - 模块 fbprophet 与 python 3.7.* 及更高版本不兼容?
- c++ - Boost Spirit X3:将(一些)空格解析为枚举