Check out the TypeScript types defined for html-validator:
declare namespace HtmlValidator {
// ...
enum ValidationResultsOutputFormats {
json = 'json',
html = 'html',
xhtml = 'xhtml',
xml = 'xml',
gnu = 'gnu',
text = 'text'
}
interface OptionsForHtmlFileAsValidationTargetAndObjectAsResult extends OptionsForHtmlFileAsValidationTarget {
format?: 'json';
}
interface OptionsForHtmlFileAsValidationTargetAndTextAsResults extends OptionsForHtmlFileAsValidationTarget {
format: 'html' | 'xhtml' | 'xml' | 'gnu' | 'text';
}
interface OptionsForExternalUrlAsValidationTargetAndObjectAsResult extends OptionsForExternalUrlAsValidationTarget {
format?: 'json';
}
interface OptionsForExternalUrlAsValidationTargetAndTextAsResults extends OptionsForHtmlFileAsValidationTarget {
format: 'html' | 'xhtml' | 'xml' | 'gnu' | 'text';
}
}
I am exploring the use of ValidationResultsOutputFormats
instead of string literals. However, both IDE and TypeScript do not provide alerts or errors for the following code:
import validateHtml, { ValidationResultsOutputFormats } from 'html-validator';
export default abstract class HtmlValidator {
public static validateHtml(compiledHtmlFile: Vinyl): void {
// ...
validateHtml({
data: compiledHtmlFile.contents.toString(),
format: ValidationResultsOutputFormats.json
}).then((validationResults: validateHtml.ParsedJsonAsValidationResults) =>
{
// ...
});
}
}
Unfortunately, as ValidationResultsOutputFormats
is undefined, a JavaScript error occurs:
Cannot read property 'json' of undefined
(referring to
format: ValidationResultsOutputFormats.json
).
Is this a mistake on my part or possibly a bug in TypeScript?
P. S. Please provide solutions that do not involve hardcoded string literals.
Update: my tsconfig.json
{
"compilerOptions": {
"target": "es6",
"strict": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"lib": [
"es2018"
],
"baseUrl": "./",
"paths": {}
}
}