ts-optchain
ts-optchain copied to clipboard
Support for complicated, nonstandard properties
Is there a way to replace _.get()
with ts-optchain
for the following use case?
const response = {
headers: {
"Content-Type": "application/json"
}
};
const contentType = _.get(response, "headers.Content-Type");
That is, while the dash is not allowed in a function name / function call likeoc(response).headers.Content-Type()
, it is still a valid object key.
Maybe a check on the argument? That is, if it matches a property in the object return that, else consider it the default argument. Examples:
// expected "application/json"
const contentType = oc(response).headers("Content-Type");
// expected 0 as default value because content-length property does not exist.
const contentLength = oc(response).headers("Content-Length", 0);
// expected empty object / default value because argument does not match one of headers' properties.
const headers = oc(response).headers({})