Saturday, March 10, 2018

optional workaround

In TypeScript adding a ? at the end of a parameter makes it optional. We see this in interfaces a lot right? Did you know that you may have it in function signatures too? This will let you get away with providing one less variable if imposed at one variable there. In JavaScript if you have a function with four variables at the signature and you only specify two when calling the function the ungiven items end up as variables which are undefined in JavaScript terms. Unfortunately, the one way in which I have found in which TypeScript is not a superset of JavaScript is that you cannot do such a trick in TypeScript as the compiler will complain. You may hack around the shortcoming with the question mark, but obviously that doesn't cure the is-not-a-superset problem, eh?

No comments:

Post a Comment