Making All Properties Required
What you'll learn: How to use the -? modifier in mapped types to remove optionality from all properties, turning them into required fields.
The Problem: Optional Properties Everywhere
When you work with objects that have optional properties (marked with ?), you sometimes need a version where every field must be present. Maybe you're receiving partial data from a form but need to validate that the complete object exists before saving to a database.
The -? Modifier: Removing Optionality
In mapped types, the -? modifier removes the optional marker from properties. Think of it like an eraser that wipes away the ? symbol:
type Person = {
name?: string;
age?: number;
email?: string;
};
type RequiredPerson = {
[K in keyof Person]-?: Person[K];
};
// Result:
// {
// name: string;
// age: number;
// email: string;
// }
How It Works
The -? says "take whatever optionality was here and remove it." Combined with a mapped type that iterates over all keys, you transform every optional property into a required one.
You can create a reusable utility:
type MakeRequired<T> = {
[K in keyof T]-?: T[K];
};
type Config = {
host?: string;
port?: number;
};
type FullConfig = MakeRequired<Config>;
// All properties now required
Note: TypeScript's built-in Required<T> utility type does exactly this — it uses -? under the hood.
Key Takeaway: The -? modifier in mapped types removes optionality from properties, ensuring all fields become required. This is the opposite of making properties optional with ?.