Feature request: NonNullable return type for parameters
Use case
Fetching a non-existing parameter usually results in an error from sdk (not sure about every provider secrets/appconfig etc). Not receiving a param usually block an execution flow. If developers need to, they can account for such errors and do some actions on catch.
However, since powertools parameters utility uses Map for caching, it is by default typed to be T | undefined, even though it's the utility that is responsible for adding the parameter to the map, and it can guarantee if something has indeed been added to the Map. Hence, is it possible to return NonNullable<T> in the get method of the params util? If it's not a desirable maybe with throwOnUndefined option?
This would greatly reduce boilerplate of checking if the returned param is not undefined or throwing an error otherwise.
Solution/User Experience
Return NonNullable<T> for get method of the params utilities.
Alternative solutions
Acknowledgment
- [x] This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- [ ] Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Hi @artkhod, thank you for opening this issue.
I think the request addresses a known papercut of using the parameters utility and even when I use it, I always find myself writing a version of this:
const param = await getParameter('foo');
if (!param) {
// do something, most likely throw an error
}
I need a bit more time to think whether this should be the default behavior or if it should have a flag like you suggested, but I'm definitely keen on considering improving this experience that customers have a way of setting the operation as not nullable, and avoid boilerplate.