Typescript TS2339:类型“Cypress&EventEmitter”上不存在属性“dayjs”
我最近重构了我的代码,使用dayJS而不是Moment.js,现在Webstorm报告了大量TS2339错误。这些错误并没有阻止我的代码编译或运行,但却使查找实际错误变得极其困难。下面是一些出现错误的代码示例:Typescript TS2339:类型“Cypress&EventEmitter”上不存在属性“dayjs”,typescript,webstorm,cypress,dayjs,Typescript,Webstorm,Cypress,Dayjs,我最近重构了我的代码,使用dayJS而不是Moment.js,现在Webstorm报告了大量TS2339错误。这些错误并没有阻止我的代码编译或运行,但却使查找实际错误变得极其困难。下面是一些出现错误的代码示例: export function verifyCreatedDate(date: string) { cy.log('Verify item creation date'); cy.get('[data-cy="timeline-date"]').should(
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文件:
我尝试将dayJS添加到这两种类型和include部分,然后重新启动WebStorm,但这也没有修复错误。我已经为此寻找了大约一周的答案,虽然这是一个常见的问题,但我发现的所有建议的解决方案都不起作用
最后,这里是我的support/index.js文件的相关部分:
您正在处理的是扩展类型,类似于您对命令所做的添加: 柏树/支架/索引d.ts:
您是否尝试过添加d.ts垫片,将dayjs添加到Cypress命名空间,类似于通常使用自定义命令所做的操作?我一直在研究这个问题,但我很难弄清楚如何添加dayjs。这些说明完全集中于添加自定义命令,在其中定义函数,我如何使用此方法向名称空间添加像DayJS这样的模块?为什么要将DayJS附加到Cypress对象?你能在你需要的文件中导入/需要它吗?我们几乎在我们的每一个规范中都使用dayJS——文档建议将它附加到Cypress对象上,在这种情况下为什么要使用dayJS?你失去了所有智能感知的东西。我试过dayjs:dayjs;但是,在suport/index.d.ts文件中导入会中断以前使用的自定义命令的自动完成。
{
"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"]
}
// 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;
declare namespace Cypress {
interface Cypress {
dayjs: any;
}
}
/// <reference types="cypress" />
declare namespace Cypress {
interface Chainable {
... your custom commands
}
import dayjs from 'dayjs';
interface Cypress {
dayjs: dayjs.Dayjs;
}
}