forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(context-async-hooks): fix eslint warnings
``` /home/runner/work/opentelemetry-js/opentelemetry-js/packages/opentelemetry-context-async-hooks/src/AbstractAsyncHooksContextManager.ts 72:35 warning Don't use `Function` as a type @typescript-eslint/ban-types 134:60 warning Don't use `Function` as a type @typescript-eslint/ban-types 152:64 warning Don't use `Function` as a type @typescript-eslint/ban-types 176:15 warning Don't use `Function` as a type @typescript-eslint/ban-types /home/runner/work/opentelemetry-js/opentelemetry-js/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts 49:22 warning Forbidden non-null assertion @typescript-eslint/no-non-null-assertion ``` The first set of warnings are more involved, the second one just didn't turn out to be necessary at all and the non-null assertion (the `!` postfix operator) can simply be removed. Explaination: When using `typeof foo === 'function'` TypeScript narrows the type of `foo` to `Function`, so it may be tempting to use `Function` in signatures when you want to accept any callable function. However, this is not quite what `Function` means. `Function` as a type in TypeScript has inherited a fair bit of historical baggage and behaves strangely. For the most part, it only guarentee that it has the `Function` prototype, so has things like `.name`, `.bind` and `.call` on it, but not much beyond that. For one thing, it includes things like class constructors which are not callable (not without the `new` keyword), but TypeScript will still let you call it, but the return value is hardcoded to `any`. At the same time though, it won't let you assign a type of `Function` to a signature type (e.g. `(...args: any[]) => any)`) without an explicit cast. So generally, `Function` probably doesn't do what you want, has massive footgun around type safety when called, and should be avoided in favor of a suitable signature type, hence the eslint rule forbidding it. Notably, depending on the position, this is often one of the few cases where you legitimately have to use `any` over `unknown` (for the parameters and/or the return type), or else the variance/ subtyping may not work out the way you want. I think there are possibly pre-exisitng issues regarding this in the files I touched, but in the interest of keeping the PR focused and not leaching changes into public API, I did not address those in this commit. Ref open-telemetry#5365
- Loading branch information
1 parent
199fd8d
commit 9c3087e
Showing
2 changed files
with
68 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters