Take the following Typescript arrow function:
/**
* Returns a probably unique component name.
*
* @param baseName a suggested name to make unique.
* @returns a probably unique name.
*/
export const getUniqueComponentName = (
baseName
): string => {
return baseName + Math.round(Math.random() * 10000000)
}
When Typescript is configured in tsconfig.json
as such:
"noImplicitAny": true,
This correctly results in a compilation error:
[ts] Parameter 'baseName' implicitly has an 'any' type.
Visual Studio Code is also smart enough to inform you about this issue during development.
My goal is to create a precommit git hook that prevents such errors from ending up in version control. I tried to do this with tslint
, husky
and lint-staged
using this npm script
:
"lint": "tslint --project tsconfig.json --config tslint.json"
However, this does not result in the compilation error showing up by tslint. It is silently ignored.
I then tried to add a rule in tslint.json:
"typedef": [
true,
"arrow-parameter"
]
While this did make tslint complain, it also started to complain in anonymous arrow functions where the tsc
compiler does not complain. In these arrow functions it should not be necessary to add types because the types were already set previously in the parent scope (they are inferred).
So basically, I would like for tslint to behave the same as tsc in this case. Anytime there is an error that would cause compilation to fail (such as the above arrow function), I would like to prevent the commit, but without actually compiling to Javascript. Is this possible?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…