Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the metricScope function parameter typing using generics #10

Merged
merged 1 commit into from
Dec 3, 2019
Merged

Preserve the metricScope function parameter typing using generics #10

merged 1 commit into from
Dec 3, 2019

Conversation

rhermes62
Copy link
Contributor

Issue #, if available:
N/A

Description of changes:
When trying to use the metricScope with function parameters, I noticed that the input arguments were being cast as any[].

This is dangerous and can lead to cases where users are able to declare something like this:

const myFunc = metricScope(metrics => async (param1: string, param2: number) => { ... });

but then (accidentally) call it like this which can cause runtime errors:

myFunc(0, 'myParam'); // Ahhhhh I accidentally put the parameters in the wrong order!!

This PR fixes this by preserving the args typing information via using generics and having the generic extend readonly unknown[] which is the immutable, type safe way to do this.

I updated existing tests to account for this (since some of them, by design, wouldnt compile with this change) as well as updated the README.md to be more clear how to use the metricScope when you want your function to have arguments.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Member

@jaredcnance jaredcnance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I agree that this is much better. Thanks for the PR!

@jaredcnance jaredcnance merged commit 0e075f7 into awslabs:master Dec 3, 2019
@rhermes62 rhermes62 deleted the PreserveMetricScopeFunctionParamaterTyping branch December 3, 2019 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants