I was not too pleased with the response I received from the expo team when they said it would be unwise to check something like this.
In my opinion, a build should fail at the very least when a name cannot be found TS2304 or cannot be found but is similar TS2552, which could lead to a runtime error.
Here is a workaround:
! npx tsc --noEmit | grep 'TS2552\|TS2304'
!
: Invert the exit code (because grep will return 0 if a match is found and 1 if not)
tsc --noEmit
: Builds without outputting to a directory (effectively to find these types of build errors)
|
: Pipe the output from the noEmit build to the next command
grep 'TS2552\|TS2304'
: Look for the TS error 2552 or 2304
(You can add any other rules to look for with \|<rule>
, the \|
is a way of escaping the or
in grep)
You can add this to your package scripts so it can be used to prefix other commands like local / deployment etc using &&
or the pre<scriptname>
syntax. If it exits 0 then &&
will continue to the next script, if it exits 1 then it will stop and fail early, the same is true for the prescript.
Example scripts / usage:
(Note the double \\
escape for JSON)
With short circuit
"scripts": {
"check:tsc": "! tsc --noEmit | grep 'TS2552\\|TS2304'",
"ios": "yarn run check:tsc && npx expo run:ios",
...
}
With prescript
Running yarn run ios
will now start with yarn run check:tsc
"scripts": {
"check:tsc": "! tsc --noEmit | grep 'TS2552\\|TS2304'",
"preios": "yarn run check:tsc",
"ios": "npx expo run:ios",
...
}
I hope this information proves helpful to someone.