As a developer working across both C# and TypeScript, I often find myself wanting familiar patterns from one language in the other. One concept that many C# developers miss when moving to TypeScript is the ability to declare a type as nullable, similar to how you’d handle nullable types in C# like int?
.
Thank me by sharing on Twitter 🙏
In C#, marking a value type as nullable is straightforward with the ?
syntax. When I started working more heavily in TypeScript, I wanted a similar way to handle nullability. Thankfully, there’s a clean and expressive way to do this, and I’ve shared this approach in a popular Stack Overflow answer to the question titled “How to declare a type as nullable in TypeScript?”.
To bring this C#-like behavior into your TypeScript projects, you can define a Nullable<T>
type like this:
type Nullable<T> = T | null;
interface Employee {
id: number;
name: string;
salary: Nullable<number>;
}
The Nullable<T>
type is a flexible solution that allows any type to be nullable, just like in C#. In the example above, the salary
property can either be a number
or null
, resembling C#’s int?
.
This pattern is not only clean but also intuitive, especially for developers coming from a C# background. It’s a simple way to bring a bit of the C# feel into your TypeScript projects while maintaining clear and maintainable code.
Highwings 4K HDMI Cable 6.6FT/2m | High-Speed HDMI 2.0 Quality Cord 4K@60Hz, Braided Nylon & Gold Connectors, HDR, Ethernet, ARC, 3D, HDCP 2.2, Compatible for PS-5/4/3/ HDTV/Monitor/DVD/Streaming
$5.99 (as of October 19, 2025 00:45 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)uni SD Card Reader, High-Speed USB C to Micro SD Card Adapter USB 3.0 Dual Slots, Memory Card Reader for SD/Micro SD/SDHC/SDXC/MMC, Compatible with MacBook Pro/Air, Chromebook, Android Galaxy
$7.99 (as of October 19, 2025 00:45 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)etguuds USB to USB C Cable 3ft, 2-Pack USB A to Type C Charger Cord Fast Charging for Samsung Galaxy A15 A25 A35 A55 A54, S24 S23 S22 S21 S20 S10 S10E, Note 20 10, Moto G, for iPhone 16 15, Gray
$5.99 (as of October 19, 2025 00:45 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)This post ties back to the Stack Overflow answer where I first introduced this idea, and it’s become a popular reference for those looking to make their TypeScript code more expressive when handling nullability.