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
noImplicitAny
strictNullChecks
strictFunctionTypes
strictBindCallApply
strictPropertyInitialization
noImplicitThis
Require Explicit any Type
Function:
Prevents implicit use of any when type cannot be inferred
Example:
// β Errorfunction log(message) { // ^^^^^^^ Parameter 'message' implicitly has an 'any' type console.log(message);}// β Correct codefunction log(message: string) { console.log(message);}// β Using genericsfunction log<T>(message: T) { console.log(message);}
If typing is difficult, use unknown. Itβs safer than any.
Strict null/undefined Checking
Function:
Prevents assigning null and undefined to other types
All types are non-nullable by default
Example:
// β Errorlet name: string = null;// ^^^^ Type 'null' is not assignable to type 'string'// β Using union typelet name: string | null = null;name = "Alice";// β Optional chaininginterface User { email?: string;}function sendEmail(user: User) { // β Error const length = user.email.length; // ^^^^^ Object is possibly 'undefined' // β Optional chaining const length = user.email?.length; // β Null check if (user.email !== undefined) { const length = user.email.length; }}
In Sonamu:
// Entity defined via entity.jsoninterface User { id: number; name: string; // Cannot be null bio: string | null; // Allows null email?: string; // Allows undefined created_at: Date;}// Ensure type safety in Model classclass UserModelClass extends BaseModelClass { @api({ httpMethod: "GET" }) async findById( subset: UserSubsetKey, id: number ): Promise<UserSubsetMapping[typeof subset] | null> { const rdb = this.getPuri("r"); const user = await rdb.table("users").where("id", id).first(); // strictNullChecks enforces null check if (!user) { return null; } return user; }}
Strict Function Type Checking
Function:
Checks contravariance of function parameters
Safer function type compatibility
Example:
interface Animal { name: string;}interface Dog extends Animal { bark(): void;}// β Error (strictFunctionTypes: true)let animalHandler: (animal: Animal) => void;let dogHandler: (dog: Dog) => void;animalHandler = dogHandler;// ^^^^^^^^^ Type '(dog: Dog) => void' is not assignable to type '(animal: Animal) => void'// β Correct directiondogHandler = animalHandler; // OK
This option significantly improves function type safety, but may have compatibility issues with some libraries.
Strict bind/call/apply Checking
Function:
Type checks parameters of bind, call, apply methods
Example:
function greet(name: string, age: number) { console.log(`Hello, ${name}! You are ${age} years old.`);}// β Errorgreet.call(null, "Alice", "30");// ^^^^ Argument of type 'string' is not assignable to parameter of type 'number'// β Correct typesgreet.call(null, "Alice", 30);// β Errorgreet.apply(null, ["Alice"]);// ^^^^^^^^^ Argument of type '[string]' is not assignable to parameter of type '[string, number]'// β Correct argument countgreet.apply(null, ["Alice", 30]);
Require Class Property Initialization
Function:
Verifies that class properties are initialized in the constructor
Example:
class User { // β Error name: string; // ^^^^ Property 'name' has no initializer and is not definitely assigned in the constructor // β Initialized in constructor email: string; constructor(email: string) { this.email = email; } // β Default value provided age: number = 0; // β Optional bio?: string; // β Allows null avatar: string | null = null; // β Definite assignment assertion (only when certain) id!: number;}
In Sonamu:
// Generated types are all initializedinterface UserRow { id: number; // Auto-generated by DB email: string; // Required field name: string; // Default value "guest" created_at: Date; // Default value CURRENT_TIMESTAMP}// Using Definite Assignment Assertion in Model classclass UserModelClass extends BaseModelClass { // ! because initialized in constructor private readonly tableName!: string; constructor() { super(); this.tableName = "users"; } @api({ httpMethod: "POST" }) async create(data: UserSaveParams) { // data must include required fields const wdb = this.getPuri("w"); return this.insert(wdb, data); }}
Require Explicit this Type
Function:
Error when this type is unclear
Example:
// β Errorfunction onClick() { console.log(this.textContent); // ^^^^ 'this' implicitly has type 'any'}// β Explicit this typefunction onClick(this: HTMLElement) { console.log(this.textContent);}// β Arrow function (no this binding)const onClick = () => { // Uses outer this};// β Class methodclass Button { text: string = "Click me"; onClick() { console.log(this.text); // OK }}
// β noUnusedLocals errorfunction greet(name: string) { const message = "Hello"; // Unused // ^^^^^^^ 'message' is declared but its value is never read return name;}// β noUnusedParameters errorfunction add(a: number, b: number) { // ^ 'b' is declared but its value is never read return a;}// β Use _ to intentionally ignorefunction onClick(_event: MouseEvent) { console.log("Clicked!");}
Parameters starting with _ donβt trigger warnings.
Require Return in All Code Paths
{ "noImplicitReturns": true}
Example:
// β Errorfunction getStatus(code: number): string { if (code === 200) { return "OK"; } else if (code === 404) { return "Not Found"; } // ^^^^ Not all code paths return a value}// β Return in all pathsfunction getStatus(code: number): string { if (code === 200) { return "OK"; } else if (code === 404) { return "Not Found"; } return "Unknown";}// β Or explicit errorfunction getStatus(code: number): string { if (code === 200) { return "OK"; } else if (code === 404) { return "Not Found"; } throw new Error(`Unknown status code: ${code}`);}
Prevent Switch Statement Fallthrough
{ "noFallthroughCasesInSwitch": true}
Example:
// β Errorfunction getDay(day: number): string { switch (day) { case 0: return "Sunday"; case 1: console.log("Monday"); // ^^^^ Fallthrough case in switch case 2: return "Tuesday"; default: return "Unknown"; }}// β Add break or returnfunction getDay(day: number): string { switch (day) { case 0: return "Sunday"; case 1: console.log("Monday"); return "Monday"; // or break; case 2: return "Tuesday"; default: return "Unknown"; }}
catch Block Variables are unknown
{ "useUnknownInCatchVariables": true}
Example:
try { throw new Error("Something went wrong");} catch (error) { // error is unknown type // β Error console.log(error.message); // ^^^^^ Object is of type 'unknown' // β Use type guard if (error instanceof Error) { console.log(error.message); } // β Type assertion (only when certain) const err = error as Error; console.log(err.message);}
Using unknown instead of any enforces type checking.
Require undefined Check for Array/Object Access
{ "noUncheckedIndexedAccess": true}
Example:
const users = ["Alice", "Bob"];// β Error (noUncheckedIndexedAccess)const firstUser = users[0];// ^^^^^^^^^ Type 'string | undefined'const name = firstUser.toUpperCase();// ^^^^^^^^^^^ Object is possibly 'undefined'// β undefined checkconst firstUser = users[0];if (firstUser !== undefined) { const name = firstUser.toUpperCase();}// β Optional chainingconst name = users[0]?.toUpperCase();// β Non-null assertion (only when certain)const name = users[0]!.toUpperCase();// Same for objectstype UserMap = { [key: string]: string };const userMap: UserMap = { alice: "Alice" };const user = userMap["alice"];// ^^^^ Type 'string | undefined'
This option makes all array/object access return T | undefined. May require many code changes.
Index signatures can only be accessed with brackets
interface Options { [key: string]: string;}const options: Options = { color: "red" };// β Errorconst color = options.color;// β Use bracketsconst color = options["color"];
When to use:
When you want to make dynamic property access explicit
exactOptionalPropertyTypes
Optional properties only allow undefined
interface User { name?: string;}// β Errorconst user: User = { name: undefined };// β Omit propertyconst user: User = {};// β To explicitly allow undefinedinterface 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.
Entire File
Single Line
Type Assertion
// @ts-nocheck// Ignore all type checks in this fileconst value = "hello";value = 123; // No error
// @ts-ignoreconst value = someUntypedLibrary();// Or add error explanation// @ts-expect-error - External library type mismatchconst value = someUntypedLibrary();
Use @ts-expect-error instead of @ts-ignore to get a warning when no error actually occurs.
// Type assertion with asconst value = someValue as string;// Non-null assertionconst element = document.getElementById("root")!;// Double assertion (last resort)const value = someValue as unknown as TargetType;
Type assertions break type safety. Use only when absolutely certain.