Structural typing, Promises, and npm are the new material; everything else maps to Python, plus one type check that must run before every merge.
Key points at a glance
- Working JavaScript is valid TypeScript, so type checking can be enabled gradually without a rewrite.
- async functions always return a Promise. If you use the result without
await, you get a Promise, not the data, so let tsc check every place that result is read. - Node can run .ts files directly, but it only strips types without checking anything, so you still need to run
tscvia annpm runscript before every merge.
On Monday morning, you get access to a client's backend repo. You have written Python for five years, and this repo is a Node project written in TypeScript. An endpoint that sends notification emails occasionally sends nothing, and the log contains only one line:
undefined
。
A Forward Deployed Engineer (FDE) cannot reply "our team doesn't do Node." The client needs this bug fixed this week, on their own stack. The good news is that many of the concepts you'll encounter have counterparts in Python. Only three things are truly new: structural typing, Promises, and npm.
You know more than you think
The official TypeScript documentation states it plainly: working JavaScript code is also TypeScript code. This has real practical value for an FDE: you can add types to client code one file at a time, without requesting a rewrite of the whole module.
The syntax is familiar too. Type annotations go after the variable name, just like Python's type hints:
function getUser(id: string)
. TypeScript can infer types in many cases, so you need to write fewer annotations than you'd expect.
any
works the same as Python's
Any
: it turns off checking. The docs discourage using it, because values of type
any
are completely unchecked.
| Python | TypeScript/Node |
|---|---|
x: str | None | x: string | null |
| Any | any (best avoided) |
| Coroutines, await | Promises, await |
| asyncio event loop | Node's event loop |
| requirements and dev-requirements | dependencies and devDependencies |
| lock file | package-lock.json |
Types are matched by shape
This is the biggest mental shift. Python compares types by class name by default. TypeScript uses structural typing: two objects with the same shape are the same type, no matter who declared them.
So in a client repo, you'll see few
class
declarations but lots of
type User = { id: string; email: string | null }. Any object with both of those fields will be accepted as
User
, including JSON freshly parsed from an API.
Union types have one rule worth remembering: TypeScript only allows an operation if it's valid for every member of the union. For
string | null
, you can't call
null
until you've confirmed the value isn't
.toLowerCase()
。
Once you enable
strictNullChecks
, any value that might be
null
or
undefined
must be checked before you use its properties or methods, just as in Python you must check for
Optional
before using a
None
value. The
strict
flag in tsconfig bundles these stricter checks together and should be turned on.
A Promise is Node's awaitable
According to MDN, a Promise is an object returned by an async function that represents the current state of an operation. If you've written asyncio, this model isn't unfamiliar: Node runs JavaScript on a single thread by default, and the event loop lets it perform non-blocking I/O.
The trap hides in a very short sentence in the docs: async functions always return a Promise. If you forget
await
when calling, what you're holding is a promise of the data, not the data itself. When reading other people's code, a missing
await
is hard to spot with the naked eye.
Fixing the client's bug step by step
Back to the email endpoint. Suppose the code looks like this:
type User = { id: string; email: string | null };
async function getUser(id: string): Promise<User | undefined> {
const row = await db.findById(id);
return row ?? undefined;
}
export async function notify(id: string) {
const user = getUser(id); // 缺少 await
await sendMail(user.email); // user 是 Promise,不是 User
}Here, even without
strict
enabled, tsc will reject the
user.email
line, because a Promise has no
property. This is the easy case: the async function's result is used on the next line, so there's a type-incorrect operation for the compiler to catch.
The other two cases are harder. If someone force-casts
user
to
any
to silence an error, tsc stops checking anything. And if a call like
sendMail(...)
loses its
await
and the result is never used, then nothing is type-incorrect, so don't expect tsc to catch it; those calls can only be reviewed by hand.
Once
await
is added, the value of
strict
becomes clear. With
strictNullChecks
enabled, tsc reports a second error:
user
has type
User | undefined
, so you can't read
yet. Fixing that exposes a third:
might be
null
. The final fix narrows the type in both places:
export async function notify(id: string) {
const user = await getUser(id);
if (!user) {
logger.warn({ id }, "未找到用户");
return;
}
if (user.email === null) {
logger.warn({ id }, "用户没有邮箱");
return;
}
await sendMail(user.email); // 此处 email 一定是 string
}Notice how you arrived at this fix: no guessing at any point. Every error tsc reported was a real case in the client's data that the old code ignored; every early
return
leaves a log line telling the client's team why the email wasn't sent.
npm is the map of the repo
Before reading any line of code, open
package.json
。
scripts
The field maps each command name to the actual command to run, so
npm run build
and
npm run test
are the standard entry points for building and testing the repo. No need to guess which tools the client uses; it's all written here.
Next, look at the two dependency groups. The npm docs point out that test frameworks, transpilers, and development-only tools should not go into
dependencies
。
They belong in
devDependencies
, just as you'd separate production dependencies from dev-requirements.
package-lock.json
locks the exact dependency tree and is designed to be committed to the repo.
If your fix changes this file, include it in the same PR.
One more new trap. Newer versions of Node can run
.ts
files directly by replacing TypeScript syntax with blanks, without doing any type checking at all.
Features that require syntax transformation (such as
enum
) will error out immediately. So "Node can run it" proves nothing about type correctness. The code above with the missing
await
runs fine under Node with no warnings; it only surfaces when tsc is invoked.
What you leave behind
The order of work for the week is shown in the diagram at the top. The last step deserves special emphasis: check whether
tsc
is in the script CI runs before merging; if not, propose adding it. Your fix solves the client's problem for this week; and the
tsc
will keep catching the same class of bug even after you move on to the next customer.
Common mistakes Python developers make
The most common is using
any
to silence the compiler, which only wipes out the information you need. The second is forgetting
await
and then going hunting for the bug in the database layer. Another habit is treating the fact that Node can run a
.ts
file as proof that the code is correct.
On the npm side, don't install build tools into
dependencies
, and don't forget to commit
package-lock.json
. Either one will leave the customer's production environment silently out of sync with your machine.
For career development, when reading FDE job postings, pay attention to how they describe the customer's tech stack: if Node or TypeScript is mentioned, those are skills you should prepare evidence for in advance. Don't just write "TypeScript" on your resume; write a concrete line: you enabled
strict
in an existing codebase, fixed a certain number of type errors, and added
tsc
to CI.
When recruiters read that line, they'll know you can deliver not only on your own tech stack but on the customer's as well.
Exercise this week: in a repo you're working on, remove the
await
from an async call whose result is read on the next line, then run tsc; repeat with a call whose result is discarded. The first time, tsc will report an error; the second time, it will say nothing—and that silence marks exactly the places you'll need to check manually at your next customer.
Was this article helpful?
Thanks for your feedback!