3

I have inherited a code base that has type errors, mostly in the tests.

Basically it appears that no 'check types' script has been run in the build pipeline.

Rather than going through and fixing all the type errors now, add a 'check types' script now and allow it to have however many errors there are. I can then gradually reduce that number.

I'm sure there's a tool that does this, but I can't find it.

1 Answer 1

5

I ended up writing a bash script to do this:

#!/bin/bash

# This script runs the tsc typecheck command, 
# And will fail if the number is above a specified threshold 
# We will slowly reduce the number until all errors are gone. 

NUM1=$1
NUM2=`expr $1 + 1`

output=`yarn tsc --pretty 2>&1`
if [ $? -eq 0 ]; then
  echo "yarn tsc rc 0, exit 0"
  exit 0
else
  errors=`yarn tsc --pretty 2>/dev/null | tail -n3 | awk NR==1 | cut -d ' ' -f2`
  if [ $errors -gt $NUM1 ]; then
    echo "Errors over $NUM1, exit 1"
    echo "Rerunning type-check so you can see the errors..."
    yarn types

    exit 1
  elif [ $errors -lt $NUM2 ]; then
    echo "Errors less than $NUM2, exit 0"
    exit 0
  fi
fi

Example usage:

./script.sh 30 

Will allow 30 errors or less, will fail on 31 errors or more.

1
  • Thanks for sharing. I wish there was a builtin checker. Just like how ESLint provides one. It is very helpful on reducing errors/warnings of a project gradually over-time. Commented Jan 16 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.