We've all hit the same wall: an interface gets defined upfront, but when we actually instantiate an object from it, not every property has a value yet. So, what's the move there?
Let's walk through a concrete example and the different tricks we can pull off to handle it.

interface CustomerDetails{
  firstName: string,
  lastName: string,
  mbNumber: string,
  adress: string
}
firstCustomer:CustomerDetails={
    firstName: 'Nikhil',
    lastName: 'Dhawan'
  }
Enter fullscreen mode Exit fullscreen mode

When we run this code, the compiler throws the following error, indicating that some required fields are absent.

Type '{ firstName: string; lastName: string; }' is missing the following properties from type 'CustomerDetails': mbNumber, address

What is the remedy for this issue?

Your initial thought is probably to set every property as optional, like this:


interface CustomerDetails{
  firstName?: string,
  lastName?: string,
  mbNumber?: string,
  address?: string
}
firstCustomer:CustomerDetails={
    firstName: 'Nikhil',
    lastName: 'Dhawan'
  }
Enter fullscreen mode Exit fullscreen mode

Rather than manually updating every interface to mark properties as optional—which does resolve the issue—there is a more refined technique available to us.


interface CustomerDetails{
  firstName: string,
  lastName: string,
  mbNumber: string,
  address: string
}
  firstCustomer:Partial<CustomerDetails>={
    firstName: 'Nikhil',
    lastName: 'Dhawan'
  }
Enter fullscreen mode Exit fullscreen mode

By leveraging Partial<>, the properties of the underlying interface become optional.

This approach lets us replicate the same outcome while keeping the original interface untouched.

I hope this added value to your learning journey. If you were already familiar with it, consider sharing this tip so others can benefit as well. For any questions or suggestions, feel free to connect with me on Twitter or drop a comment below. Until next time, happy coding—and please share your feedback!