首页 > 解决方案 > 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;

标签: typescriptwebstormcypressdayjs

解决方案


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;
    }
}

推荐阅读