CodePark/ questions / TypeScript on the server
START PRACTICINGPRACTICE
Backend question · TypeScript › TypeScript on the server

With @types/node installed and strict on, what is the type of port?

const port = process.env.PORT;
string | undefined

The type is string | undefined. Node's types declare every process.env value this way, for two reasons:
• Any variable may be unset, so it can be
undefined.
• Environment values are always strings.


It is never
number. A common pattern is to parse process.env once at startup with a schema, for example z.coerce.number(). Then you export a typed config object for the rest of the app.