by uk_programmer · 6y ago · 0 comments
However it is worth keeping the following in mind. The compiler will check that in your code that these properties are present and assigned correctly. However at runtime nothing is guaranteed especially when dealing with the DOM.
One of the things that I don't like about TypeScript (I have written a fair bit of it) is that you believe you have type safety when it is really type hinting.
function someFunction(obj?:DuckType) {
//Snip other logic
someOtherFunction(obj);
}
function someOtherFunction(obj:DuckType) {
}
The type checker catch that. So you still have to do: function someFunction(obj?:DuckType) {
obj = obj || { /* set some object properties */
//Snip other logic
someOtherFunction(obj);
}
function someOtherFunction(obj:DuckType) {
}
I have found plenty of examples where people haven't set a default value because the compiler hasn't flagged anything wrong with the code and you get an uncaught reference error.