Optional properties are marked with ?
and may or may not be present in the object:
interface Config {
apiUrl: string;
timeout?: number; // Optional
retries?: number; // Optional
debug?: boolean; // Optional
}
const config: Config = {
apiUrl: "https://api.example.com"
// Other properties can be omitted
};
// Function with optional parameters
function createUser(name: string, age?: number): User {
return {
name,
age: age ?? 18 // Default value if not provided
};
}