Optional properties must be accessed with care. Either use optional chaining:
Or use default values:
This ensures your code does not crash if the property is undefined.
asked about 1 year ago
1
72
I am working with interfaces in TypeScript, and I want some properties to be optional. However, when I use them later in code, TypeScript still throws errors saying the property might be undefined. What is the right way to deal with optional props in a safe and clean way?
Optional properties must be accessed with care. Either use optional chaining:
Or use default values:
This ensures your code does not crash if the property is undefined.
1