微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TS2339:“Cypress & EventEmitter”类型上不存在属性“dayjs”

如何解决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;
    }
}
,

你正在搞乱的是扩展类型,类似的添加就像你对命令所做的一样:https://docs.cypress.io/guides/tooling/typescript-support#Types-for-custom-commands

declare namespace Cypress {
  interface Cypress {
    dayjs: any;
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。