Two people open the same test file. One sees three red lines in the editor. The other sees none. Both have the same code.
They have different settings. Every check in this course depends on a file called tsconfig.json, and a project with a loose one lets most of the examples you have read compile.
This lecture has no practice exercise. The settings belong to a whole project, and the practice editor on this site checks one small set of files with settings you cannot change.
What the file is
tsconfig.json sits in the root folder of a TypeScript project. The compiler and the editor both read it. It answers two questions: which files to check, and how.
Here is a reasonable one for a folder of Playwright tests.
{
"compilerOptions": {
"strict": true,
"noEmit": true,
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"skipLibCheck": true
},
"include": ["tests/**/*.ts", "playwright.config.ts"]
}
Take the settings one at a time.
strict
strict is one switch for a group of checks. You have relied on four of them.
| Part of strict | What it reports | Where you met it |
|---|---|---|
noImplicitAny |
A parameter with no type | Junior lecture 2 |
strictNullChecks |
A value that may be undefined or null |
Junior lecture 6 |
strictPropertyInitialization |
A class field the constructor does not fill in | Lecture 5 |
useUnknownInCatchVariables |
A caught error used without a check | Lecture 1 |
Here is the first one, with the settings this site uses.
function isOverdue(daysLate) {
return daysLate > 0;
}
fees.ts(1,20): error TS7006: Parameter 'daysLate' implicitly has an 'any' type.
Without strict, that function compiles, and daysLate is any.
Turn strict on in a new project on the first day. Turning it on later in a large suite produces hundreds of errors at once, and teams often give up halfway. If you join a project where it is off, that is worth knowing before you trust a clean compile.
noEmit
You learned that the compiler does two jobs: it checks, and it writes JavaScript. noEmit switches the second job off. In a Playwright project the test runner turns the files into JavaScript itself, so the compiler is there only to check.
target, module and moduleResolution
target says which version of JavaScript the code is written for. It decides which built-in features the compiler knows about.
module and moduleResolution say how import lines are understood. They decide, among other things, whether "./fees" with no file extension is accepted. This is the setting the Junior lecture on modules referred to. Use the values your framework's documentation gives, and do not mix advice from different sources.
skipLibCheck
skipLibCheck tells the compiler not to check the type files inside installed packages. Those are other people's code. Checking them is slow, and an error there is not yours to fix.
include
include lists the files to check. If a folder of tests is missing from it, those files are not checked, and they show no errors. An empty error list can mean correct code or an unchecked file. When a file you know is wrong shows nothing, look here first.
The test runner does not apply these settings
This part comes from the Playwright documentation, and it is the first lecture of the Junior track again. Playwright turns TypeScript into JavaScript and runs it. It does not check types. It reads only a few options from tsconfig.json, such as baseUrl and paths, which affect how imports are found. strict is not among them.
So everything in the table above is applied by tsc and by your editor, and by nothing else. A suite can be green in Playwright with strict errors on every page.
That is why the check needs its own command, in the same place as lint.
npx tsc -p tsconfig.json --noEmit
Run it in the build of every pull request, before the tests. Then a type error stops the build as a failing test would. The Playwright documentation also recommends a separate tsconfig.json inside the tests folder, so the test code can have its own settings.
Two settings to know about
Neither of these is part of strict. Each one turns a surprise from this course into a compiler error.
noUncheckedIndexedAccess is about lists. With it on, reading titles[1] gives string | undefined, so the surprise from the Junior lecture on arrays is reported. It asks for more checks in your code. Many test projects find that worth it.
exactOptionalPropertyTypes is about optional fields. In lecture 4, buildBook({ copies: undefined }) compiled and returned a book with no copies value. With this setting on, an optional field may be left out, and it may not be set to undefined by hand. Here is the call from lecture 4 again, compiled with the setting on.
interface Book {
id: number;
title: string;
copies: number;
}
function buildBook(overrides: Partial<Book> = {}): Book {
return { id: 1, title: "Dune", copies: 3, ...overrides };
}
buildBook({ copies: undefined });
books.ts(11,11): error TS2379: Argument of type '{ copies: undefined; }' is not assignable to parameter of type 'Partial<Book>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
Types of property 'copies' are incompatible.
Type 'undefined' is not assignable to type 'number'.
Read the last line first. With the setting on, the compiler refuses the call that broke the builder's return type.
Remember this
tsconfig.json decides which files are checked and how strictly. Turn on strict and noEmit for a test project. Playwright does not apply these settings, so run tsc --noEmit as its own step, and make sure include covers every test file.
Reference: What is a tsconfig.json in the TypeScript Handbook.