In the realm of JavaScript, it is not feasible as it does not function in that context either. For instance, consider this snippet of JS:
const foo = ({bar}) => {
...
};
foo({ bar: 123 }); // runs successfully
foo(); // throws an error
Attempting to call foo
sans an object parameter will yield an exception like so:
Uncaught TypeError: Cannot destructure property `bar` of 'undefined' or 'null'.
This occurs due to the similarity between the aforementioned function implementation and this equivalent representation:
const foo = (arg1) => {
let bar = arg1.bar;
};
foo();
The resulting exception would be nearly identical because arg1.bar
seeks to access a property on an object that is deemed undefined
.
To put it succinctly, at runtime, JavaScript lacks recognition of "optional" parameters, leading TypeScript to follow suit.