Skip to main content
TypeScript’s type checking options determine the stability of your code. Sonamu enables strict mode by default to provide maximum type safety.

Strict Mode

Sonamu enables all strict options.
{
  "compilerOptions": {
    "strict": true,                          // Enable all strict options
    "noImplicitAny": true,                   // Require explicit any type
    "strictNullChecks": true,                // Strict null/undefined checking
    "strictFunctionTypes": true,             // Strict function type checking
    "strictBindCallApply": true,             // Strict bind/call/apply checking
    "strictPropertyInitialization": true,    // Require class property initialization
    "noImplicitThis": true,                  // Require explicit this type
    "alwaysStrict": true                     // Auto-add 'use strict'
  }
}
strict: true is a shortcut that enables all the above options at once.

Strict Options in Detail

Require Explicit any Type

Function:
  • Prevents implicit use of any when type cannot be inferred
Example:
// ❌ Error
function log(message) {
  //         ^^^^^^^ Parameter 'message' implicitly has an 'any' type
  console.log(message);
}

// βœ… Correct code
function log(message: string) {
  console.log(message);
}

// βœ… Using generics
function log<T>(message: T) {
  console.log(message);
}
If typing is difficult, use unknown. It’s safer than any.

Additional Type Check Options

Sonamu enables additional check options beyond strict mode.
{
  "compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "useUnknownInCatchVariables": true,
    "noUncheckedIndexedAccess": true
  }
}

Warn About Unused Code

{
  "noUnusedLocals": true,
  "noUnusedParameters": true
}
Example:
// ❌ noUnusedLocals error
function greet(name: string) {
  const message = "Hello";  // Unused
  //    ^^^^^^^ 'message' is declared but its value is never read
  return name;
}

// ❌ noUnusedParameters error
function add(a: number, b: number) {
  //                     ^ 'b' is declared but its value is never read
  return a;
}

// βœ… Use _ to intentionally ignore
function onClick(_event: MouseEvent) {
  console.log("Clicked!");
}
Parameters starting with _ don’t trigger warnings.

Stricter Options (Optional)

Options you can add as needed.
{
  "compilerOptions": {
    "noPropertyAccessFromIndexSignature": true,
    "exactOptionalPropertyTypes": true,
    "noUncheckedSideEffectImports": true
  }
}
Index signatures can only be accessed with brackets
interface Options {
  [key: string]: string;
}

const options: Options = { color: "red" };

// ❌ Error
const color = options.color;

// βœ… Use brackets
const color = options["color"];
When to use:
  • When you want to make dynamic property access explicit
Optional properties only allow undefined
interface User {
  name?: string;
}

// ❌ Error
const user: User = { name: undefined };

// βœ… Omit property
const user: User = {};

// βœ… To explicitly allow undefined
interface User {
  name: string | undefined;
}
When to use:
  • When you want to strictly distinguish between optional and | undefined
May have compatibility issues with many libraries.

Disabling Type Checks

Sometimes you need to bypass type checks in specific situations.
// @ts-nocheck
// Ignore all type checks in this file

const value = "hello";
value = 123;  // No error
{
  "compilerOptions": {
    // Use Sonamu default settings
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "useUnknownInCatchVariables": true,
    "noUncheckedIndexedAccess": true
  }
}
For new projects, enable all options.

Performance Considerations

More type check options can increase compilation time.
{
  "compilerOptions": {
    "skipLibCheck": true,           // Skip node_modules type check
    "incremental": true,             // Incremental compilation
    "tsBuildInfoFile": ".tsbuildinfo"
  }
}
skipLibCheck
  • Don’t check .d.ts files in node_modules
  • Significantly reduces compilation time
  • Enabled by default in Sonamu
incremental
  • Only recompile changed files
  • Cache stored in .tsbuildinfo file

Next Steps