| name | typescript-strict |
| description | Strict TypeScript practices. Use when writing TypeScript code to ensure type safety. |
Enforce strict TypeScript practices:
No any
Never use any. Use unknown for truly unknown types:
function process(data: any) { }
function process(data: unknown) {
if (typeof data === 'string') {
}
}
Explicit Return Types
Add explicit return types for public/exported functions:
function calculateTotal(items: Item[]): number {
return items.reduce((sum, item) => sum + item.price, 0)
}
async function fetchUser(id: string): Promise<User | null> {
}
Interface over Type
Prefer interface for object shapes (better error messages, extendable):
interface User {
id: string
name: string
email: string
}
type Status = 'pending' | 'active' | 'done'
type Nullable<T> = T | null
Readonly
Mark immutable data as readonly:
interface Config {
readonly apiUrl: string
readonly maxRetries: number
}
function processItems(items: readonly Item[]) {
}
Type Guards
Use type guards for runtime type checking:
function isUser(value: unknown): value is User {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
'name' in value
)
}
if (isUser(data)) {
console.log(data.name)
}
Const Assertions
Use as const for literal types:
const STATUSES = ['pending', 'active', 'done'] as const
type Status = typeof STATUSES[number]
Non-null Assertion
Avoid ! when possible. Use optional chaining or type guards:
const name = user!.name
const name = user?.name ?? 'Unknown'