Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

protractor - Typescript Error:TS2345: Argument of type '{ theme: string; jsonFile: string; output: string; }; }' is not assignable to parameter of type 'Options'

I am getting the error in the title from the following code:

import * as fs from 'fs'
import { mkdirp } from 'mkdirp'
import * as report from 'cucumber-html-reporter'
const Cucumber = require('cucumber')

export class CucumberReportExtension {

    private static jsonDir = __dirname + '/../../test/reports/json';
    private static htmlDir = __dirname + '/../../test/reports/html';
    private static jsonFile = CucumberReportExtension.jsonDir + '/cucumber_report.json';

    private static cucumberReporterOptions = {
        theme: 'bootstrap',
        jsonFile: CucumberReportExtension.jsonFile,
        output: CucumberReportExtension.htmlDir + '/cucumber_reporter.html',
        reportSuiteAsScenarios: true,
        launchReport: true,
        metadata: {
            'App Version':'1.0.0',
            'Test Environment': 'Development',
            Browser: 'Chrome  84.0.4147.105',
            Platform: 'Windows 10',
            Parallel: 'Scenarios',
            Executed: 'Local'
        }
    }

    public static CreateReportFile(dirName) {
        // Check of the directory exist
        if (!fs.existsSync(dirName))
            mkdirp.sync(dirName);
    }

    public static GenerateCucumberReport(){

        report.generate(CucumberReportExtension.cucumberReporterOptions);
    }

}

My package.json contains:

"dependencies": {
    "@types/jasmine": "^3.5.11",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "^14.0.14",
    "faker": "^5.1.0",
    "jasmine": "^3.5.0",
    "lodash": "^4.17.20",
    "log4js": "^6.3.0",
    "log4js-protractor-appender": "^1.1.2",
    "mkdirp": "latest",
    "moment": "^2.28.0",
    "pg": "^8.3.3",
    "pg-hstore": "^2.3.3",
    "protractor": "^7.0.0",
    "qs": "^6.9.4",
    "stringinject": "^2.1.1"
  },
  "devDependencies": {
    "@babel/core": "^7.10.4",
    "@babel/preset-env": "^7.10.4",
    "@types/chai": "^4.2.11",
    "@types/cucumber": "^6.0.1",
    "@types/load-json-file": "^5.1.0",
    "axios": "^0.19.2",
    "babel-loader": "^8.1.0",
    "chai": "^4.2.0",
    "chai-as-promised": "^7.1.1",
    "chai-smoothie": "^0.3.2",
    "core-js": "^3.6.5",
    "cucumber": "^6.0.5",
    "cucumber-html-reporter": "^5.2.0",
    "https": "^1.0.0",
    "k6": "0.0.0",
    "load-json-file": "^6.2.0",
    "multiple-cucumber-html-reporter": "^1.16.3",
    "nodejs-nodemailer-outlook": "^1.2.3",
    "protractor-cucumber-framework": "^6.2.1",
    "request": "^2.88.2",
    "ts-node": "^8.10.2",
    "tslint": "^6.1.3",
    "tslint-etc": "^1.13.6",
    "typescript": "^3.9.7",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.12"
  }
}
question from:https://stackoverflow.com/questions/65947413/typescript-errorts2345-argument-of-type-theme-string-jsonfile-string-ou

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From looking at the expected type for the Options object passed to report.generate, I can see that the type which they expect for theme is a union of string literals.

Your theme 'bootstrap' is one of those options, but in the error message you can see that typescript interpreted it as string instead of the literal string 'bootstrap'. You can use as const so that typescript sees it as a literal:

private static cucumberReporterOptions = {
     theme: 'bootstrap' as const,
     ....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...