Imagine having an interface structured as follows:
class Something {
constructor(things) {
if (things) {
doSomething();
} else return { errorCode: 1 }
}
}
Does this code appear to be correct? When using TypeScript, I encounter an error message stating
property errorCode does not exist on type Something
. What could be a different method to handle this situation? Here is an alternative solution that comes to mind:
class Something {
constructor(things) {
if (things) {
doSomething();
} else this.error = { errorCode: 1 }
}
}
Both options seem to achieve the same outcome. However, I am curious to understand which approach is more optimal.