Loading…
Loading documentation...
Start a TypeScript project with the strict settings the compiler now proposes, read the first ten errors they raise, and fix each one the right way.
Commands checked on 2026-10-13 with TypeScript 5.9, Node.js 24, npm 11.
A small TypeScript project whose compiler settings refuse the mistakes that cause most production bugs: a value that may be missing, a list read past its end, an error of unknown shape. You will have seen each refusal once, fixed it, and run the result with Node.js without a build step.
node -v and npm -v.npm init -y
npm install -D typescript@5.9 @types/node@24
npx tsc --init
tsc --init writes tsconfig.json and prints Created a new tsconfig.json.
Open tsconfig.json. Without its comments it holds:
{
"compilerOptions": {
"module": "nodenext",
"target": "esnext",
"types": [],
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"strict": true,
"jsx": "react-jsx",
"verbatimModuleSyntax": true,
"isolatedModules": true,
"noUncheckedSideEffectImports": true,
"moduleDetection": "force",
"skipLibCheck": true
}
}
Three of these lines do most of the work in this guide:
strict turns on a family of checks, the most useful being that null and undefined are not silently part of every type.noUncheckedIndexedAccess makes list[0] a value that may be missing, because it may be.exactOptionalPropertyTypes makes an optional property mean "may be left out", not "may be set to undefined".Create src/user.ts:
type User = { id: number; name: string; email?: string }
const users: User[] = [{ id: 1, name: 'Ada', email: 'ada@example.com' }, { id: 2, name: 'Linus' }]
export function findUser(id) {
return users.find((user) => user.id === id)
}
export function emailDomain(id: number): string {
const user = findUser(id)
return user.email.split('@')[1]
}
export function firstName(names: string[]): string {
return names[0].toUpperCase()
}
export function parsePort(value: string): number {
try {
return Number.parseInt(value, 10)
} catch (error) {
throw new Error(error.message)
}
}
npx tsc --noEmit
It prints ten errors. TS1287 appears four times, once for every export, and TS2322 repeats the cause of the two TS18048 lines. The six that tell the story:
src/user.ts(5,1): error TS1287: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled.
src/user.ts(5,26): error TS7006: Parameter 'id' implicitly has an 'any' type.
src/user.ts(11,10): error TS18048: 'user' is possibly 'undefined'.
src/user.ts(11,10): error TS18048: 'user.email' is possibly 'undefined'.
src/user.ts(15,10): error TS2532: Object is possibly 'undefined'.
src/user.ts(22,21): error TS18046: 'error' is of type 'unknown'.
TS1287 appears once for every export. The project's package.json does not say whether its files are ES modules, so Node.js treats them as CommonJS, where export does not exist. Say it:
npm pkg set type=module
Replace src/user.ts with:
type User = { id: number; name: string; email?: string }
const users: User[] = [{ id: 1, name: 'Ada', email: 'ada@example.com' }, { id: 2, name: 'Linus' }]
export function findUser(id: number): User | undefined {
return users.find((user) => user.id === id)
}
export function emailDomain(id: number): string | undefined {
const user = findUser(id)
return user?.email?.split('@')[1]
}
export function firstName(names: string[]): string {
const first = names[0]
if (first === undefined) throw new Error('names is empty')
return first.toUpperCase()
}
export function parsePort(value: string): number {
const port = Number.parseInt(value, 10)
if (!Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error(`not a port: ${value}`)
}
return port
}
export function describe(error: unknown): string {
return error instanceof Error ? error.message : String(error)
}
What changed, error by error:
TS7006, a parameter with no type. id is a number, so it says so. The return type is written too: a user may not be found.TS18048, a value that may be missing. There may be no such user, and a user may have no email. ?. stops at the first missing value, and the function now admits that it may return nothing.TS2532, a list read past its end. An empty list has no first name. The code checks and says what went wrong.TS18046, an error of unknown shape. Anything can be thrown, not only an Error. describe checks before it reads .message.parsePort hid a bug no setting catches: Number.parseInt('eighty', 10) does not throw, it returns NaN. The new one checks the result.Node.js 24 runs TypeScript files directly by removing the types. Create src/main.ts:
import { describe, emailDomain, firstName, parsePort } from './user.ts'
console.log(emailDomain(1), emailDomain(2), emailDomain(3))
console.log(firstName(['ada', 'linus']))
console.log(parsePort('8080'))
try {
parsePort('eighty')
} catch (error) {
console.log(describe(error))
}
The import names the file as it is on disk, ./user.ts. The compiler refuses that until you tell it nothing will be emitted:
src/main.ts(1,61): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
Add two lines to compilerOptions in tsconfig.json:
"noEmit": true,
"allowImportingTsExtensions": true,
npm pkg set scripts.typecheck="tsc --noEmit"
npm run typecheck
node src/main.ts
typecheck prints no error. The program prints:
example.com undefined undefined
ADA
8080
not a port: eighty
TS2591: Cannot find name 'process'. The proposed "types": [] loads no global types, Node's included. When your code uses process, Buffer or other Node globals, change the line to "types": ["node"].TS2375 on an object with nickname: undefined. That is exactOptionalPropertyTypes at work. Leave the property out, or declare it as nickname?: string | undefined when "present but empty" is a real state.TS5052: Option 'exactOptionalPropertyTypes' cannot be specified without specifying option 'strictNullChecks'. Someone tried to switch strict off while keeping the rest. The options build on each other; keep strict on.any or !. any switches the checks off for everything the value touches, and ! tells the compiler a value exists without making it so. Both turn a compile error into a production error. Narrow with a check instead.unknown until you have checked its shape.tsc --noEmit in your checks even when a bundler builds the project: many build tools remove types without checking them.Every Servers Are Up virtual machine and container includes an integrated HTML5 browser-based SSH, RDP, and VNC console in your dashboard, default-off inbound firewall rules with per-IP allow-lists, and full programmatic control via REST API v1 and Model Context Protocol (MCP). Explore our Virtual Datacenter Plans or reach out to Servers Are Up Support.