strict: true
- TypeScript course
- Beginner
- Lesson 67/89
- 4 min
Learn the strict flag in tsconfig.
What is strict: true?
Setting the strict flag to true in tsconfig.json turns on all of TypeScript's strict type-checking options at once. This catches the most common programming errors at compile time.
{
"compilerOptions": {
"strict": true
}
}This single line enables many separate options together.
Which options are included?
With strict: true, the following options are enabled together: strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, noImplicitAny, noImplicitThis, alwaysStrict, and useUnknownInCatchVariables. You do not need to enable each one separately.
{
"compilerOptions": {
"strict": true,
"target": "ES2020"
}
}Practice Strict config check
Define a TsConfig interface: a compilerOptions field containing strict: boolean and target: string. Create a config object with strict: true and target: "ES2020". The function strictEnabled(c: TsConfig): boolean should return the strict value and print the result.
Starter code
// TsConfig arayüzünü tanımla: compilerOptions { strict: boolean, target: string }
// config nesnesini oluştur: strict true, target ES2020
// strictEnabled(c: TsConfig): boolean döndürür
// strict değerini yazdırSample solution
interface TsConfig {
compilerOptions: {
strict: boolean;
target: string;
};
}
const config: TsConfig = {
compilerOptions: {
strict: true,
target: "ES2020"
}
};
function strictEnabled(c: TsConfig): boolean {
return c.compilerOptions.strict;
}
console.log(strictEnabled(config));Expected output
true
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does setting strict: true do in tsconfig.json?
- Automatically generates JavaScript code
- Turns on many strict type checks at once Correct answer
- Only enables null checking
- Disables all type checks
Explanation: strict: true enables many strict checks such as strictNullChecks, strictFunctionTypes, and strictPropertyInitialization all at once.
2 Complete the strict flag
"strict": ____,
"strict": true,
Explanation: The strict flag takes the value true; this turns on all strict options. false turns them off; "yes" or 1 are not the right type.
3 strict mode parts
| Term | Meaning |
|---|---|
strict: true |
Turns on all strict checks |
noImplicitAny |
Forbids implicit any |
alwaysStrict |
Adds 'use strict' to every file |
tsconfig.json |
TypeScript configuration file |