A Shelf test file has a red line in the editor. You run the tests anyway, and every test passes. Later the build of your pull request fails on that same line.
Both results are correct. They come from two tools that answer two different questions. This lecture is about that difference, and the rest of the track depends on it.
The same code, twice
Here is a small JavaScript function. It works out a late fee for a book.
function lateFee(daysLate, dailyFee) {
return daysLate * dailyFee;
}
const days = "4";
console.log(lateFee(days, 0.5));
2
days is text, because a test read it from the page. JavaScript turns "4" into the number 4 for the multiplication, so the answer is right. Nothing tells you that text went into a calculation.
Now the same code in TypeScript. TypeScript is JavaScript with types added. A type describes the values a piece of code accepts. The : number after a name is a type annotation. It says that this name holds a number.
function lateFee(daysLate: number, dailyFee: number): number {
return daysLate * dailyFee;
}
const days = "4";
console.log(lateFee(days, 0.5));
fees.ts(6,21): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
2
There are two results here, and both are real.
The first box is from the compiler, the program that reads TypeScript. It found a type error. Read the line from left to right: the file, then the line and column in brackets, then a code, then a sentence. Line 6 passes text where the function asks for a number.
The second box is what the code printed when it ran. It printed 2, the right answer.
Why the code still ran
The compiler does two separate jobs.
- It checks the types. The result is a list of errors, or no errors.
- It removes the types. The result is plain JavaScript.
A browser and Node run only JavaScript. They never see a type. This is the code they get from the example above:
function lateFee(daysLate, dailyFee) {
return daysLate * dailyFee;
}
const days = "4";
console.log(lateFee(days, 0.5));
That is the first example again, character for character. The annotations are gone, so the code behaves as the JavaScript did.
Two facts follow, and you will use them in every lecture of this track.
- A type never changes what the code does when it runs.
- The second job does not wait for the first. Code with a type error can still be turned into JavaScript and run.
Your test runner may skip the check
This part is about Playwright, and it comes from the Playwright documentation. Playwright runs TypeScript test files directly. It turns them into JavaScript and runs them. It does not check the types, and it runs the tests even when the code has type errors.
So a green npx playwright test tells you nothing about types. The Playwright documentation recommends running the compiler as a separate step:
npx tsc -p tsconfig.json --noEmit
tsc is the TypeScript compiler. --noEmit means "check the types and write no files". Other test runners make their own choice here, so read the documentation of the one you use.
Teams often meet this as a surprise. The tests pass on a laptop, and the build fails, because only the build has a tsc step. The opposite is worse. A project has no tsc step at all, type errors collect for months, and the red lines in the editor stop meaning anything.
The other direction
A clean type check does not mean the code is correct.
function lateFee(daysLate: number, dailyFee: number): number {
return daysLate + dailyFee;
}
console.log(lateFee(4, 0.5));
4.5
The compiler reports nothing. Adding two numbers gives a number, and that is all it checks. The fee is wrong, because the function adds where it should multiply. Only a test that runs the code can find that.
| Question | Who answers it | When |
|---|---|---|
| Do the values fit the types? | The compiler, in tsc and in your editor |
Before the code runs |
| Does the code do the right thing? | Your tests | While the code runs |
You need both answers. Neither one gives you the other.
Remember this
TypeScript checks types before the code runs, then removes them. A type error is not a test failure, and a passing test does not mean the types are right. Playwright does not check types, so run tsc --noEmit as its own step.
Reference: The Basics in the TypeScript Handbook.