After declaring two variables param01
and param02
I go ahead and check if the incoming event.pathParameters
is not undefined
:
let param01, param02;
if (event.pathParameters!=undefined) {
if (event.pathParameters.param01!=undefined) {
param01 = event.pathParameters.param01;
}
if (event.pathParameters.param02!=undefined) {
param02 = event.pathParameters.param02;
}
}
While it works fine, it takes 10 lines of code. I wonder if there is a shorter and more elegant way of getting it done in Typescript
You can try something like this:
const param01 = event.pathParameters && event.pathParameters.param01 || null;
const param02 = event.pathParameters && event.pathParameters.param02 || null;
or using just one line:
const { param01, param02 } = event.pathParameters || {};
If your environment allows, you can also use Optional chaining .
const param01 = event.pathParameters?.param01; // value or undefined
const param02 = event.pathParameters?.param02; // value or undefined
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.