There is an issue with // @ts-expect-error
where it can hide other errors inside of:
- React property assignments
type Props = {
foo: string;
baz: number;
}
// takes Props
<Component
// @ts-expect-error - ...
foo="bar"
// we're missing a prop `baz`, but that's being hidden
/>
- Object literals
type MyShape = {
foo: number;
baz: string;
}
const blah: MyShape = {
// @ts-expect-error - expected number, got string
foo: "bar",
// missing key `baz`, but we don't see an error
badKey: "blah", // bad key, but we don't see an error
}
You'd think it would just suppress 1 line, but it ends up suppressing every error inside the component/object declaration.
See this TS playground link for examples of errors being hidden.
If you want to lint some TS files for problem spots, you can use the attached code which builds an AST of each file and extracts any ts-expect-error
comments from the two mentioned contexts above:
import fs from 'fs';
import {lintExpectErrorForFileContents} from './findBadTsExpectErrors.ts';
// You could build this list up with the `glob` library, if you wanted
// and run it on every file in your codebase.
const filesToLint = [
"src/foo/bar.tsx",
"src/blah/baz.ts",
];
let foundErrors = false;
filesToLint.forEach((fileName) => {
const contents = fs.readFileSync(fileName).toString();
const result = lintExpectErrorForFile(fileName, contents);
if (!result.isValid()) {
foundErrors = true;
result.errors.forEach((error) => {
const { characterNumber, lineNumber, text } = error;
// Output path:lineNumber:characterNumber:errorText
console.log(
[
fileName,
lineNumber,
characterNumber,
text,
].join(':'),
)
});
}
}
if (!foundErrors) {
console.log("No bad code spots found, awesome!");
}
This output format should work with Vim's quickfix function, if you want to write a small function to import the results of this command to your quickfix window.