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

chore(typescript): enable useUnknownInCatchVariables in core #3211

Merged
merged 1 commit into from Jan 27, 2022

Conversation

rwaskiewicz
Copy link
Member

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Unit tests (npm test) were run locally and passed
  • E2E Tests (npm run test.karma.prod) were run locally and passed
  • Prettier (npm run prettier) was run locally and passed

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe): TypeScript compiler changes

What is the current behavior?

Errors declared in catch blocks are implicitly typed as any

GitHub Issue Number: N/A

What is the new behavior?

this commit enables the 'useUnknownInCatchVariables' flag in the core
compiler's tsconfig.json file. the remainder of the changes related
directly to errors emitted by the TypeScript compiler as a result of
this change.

the most common change in this commit is updating a catch block that
consists of a single call to stencil's catchError function. because
catchError may theoretically accept a value or object that does not
conform to the Error interface, we annotate many of the error
variables declared in the catch block as type any, in a good-faith
effort not to lose any useful information that we would otherwise turn
into a Diagnostic. Using a type guard or other type narrowing
mechanism would be feasible if any value that is not of type Error
were logged in some other way. However in an attempt to not pollute the
codebase with two different logging paths, we accept the ambiguity of
catchError and catching errors of type any for the time being.

Does this introduce a breaking change?

  • Yes
  • No

Testing

The TS Compiler was run over the codebase, to ensure no type-safety issues. Admittedly it's not the most comprehensive, but covers most of the cases throughout this patch.

Other information

Do not merge please. Relies on #3205

@rwaskiewicz rwaskiewicz marked this pull request as ready for review January 24, 2022 18:49
@rwaskiewicz rwaskiewicz requested a review from a team January 24, 2022 18:49
throw realpathErr;
} catch (realpathErr: unknown) {
if (realpathErr instanceof Object && 'code' in realpathErr) {
// @ts-ignore: we've determined 'code' is in the prototype chain, but TS isn't happy about the access
Copy link
Member

Choose a reason for hiding this comment

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

FYI: This is because in isn't a type guard: microsoft/TypeScript#21732. One option would be to implement a type guard function for NodeJS.ErrnoException:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/globals.d.ts#L183-L188

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call, impl in f0fb959..b88604b

@@ -139,9 +139,12 @@ export const createCustomResolverSync = (
const fsFilePath = normalizeFsPath(p);
try {
return sys.realpathSync(fsFilePath);
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to your changes... does this even throw an exception? According to the interface of sys.realpathSync() it returns a CompilerSystemRealpathResults and doesn't throw an exception:

/**
* SYNC! Does not throw.
*/
realpathSync(p: string): CompilerSystemRealpathResults;

Copy link
Member Author

Choose a reason for hiding this comment

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

It shouldn't, although I think the problem here is that we can't guarantee implementations on that interface won't throw. Perhaps this is just a bit of defensive programming?

Base automatically changed from ts4.5 to main January 27, 2022 18:47
this commit enables the 'useUnknownInCatchVariables' flag in the core
compiler's tsconfig.json file. the remainder of the changes related
directly to errors emitted by the TypeScript compiler as a result of
this change.

the most common change in this commit is updating a `catch` block that
consists of a single call to stencil's `catchError` function. because
`catchError` may theoretically accept a value or object that does not
conform to the `Error` interface, we annotate many of the error
variables declared in the `catch` block as type `any`, in a good-faith
effort not to lose any useful information that we would otherwise turn
into a `Diagnostic`. Using a type guard or other type narrowing
mechanism would be feasible if any value that is not of type `Error`
were logged in some other way. However in an attempt to not pollute the
codebase with two different logging paths, we accept the ambiguity of
`catchError` and catching errors of type `any`  for the time being.
@rwaskiewicz rwaskiewicz merged commit d9f87ac into main Jan 27, 2022
@rwaskiewicz rwaskiewicz deleted the useUnknownInCatchVars branch January 27, 2022 19:42
rwaskiewicz added a commit that referenced this pull request Mar 2, 2022
this commit is a quick fix for the browser-compile tests that currently
do not compile after setting `useUnknownInCatchVariables` in
#3211. This went unnoticed as
this application is not currently type checked by our CI process. In
order to get the application to compile, we type the error as `any` for
now, with the intent of properly narrowing it in STENCIL-371 (and adding
proper CI checks there as well). this commit only unblocks next monday's
release.
rwaskiewicz added a commit that referenced this pull request Mar 2, 2022
this commit is a quick fix for the browser-compile tests that currently
do not compile after setting `useUnknownInCatchVariables` in
#3211. This went unnoticed as
this application is not currently type checked by our CI process. In
order to get the application to compile, we type the error as `any` for
now, with the intent of properly narrowing it in STENCIL-371 (and adding
proper CI checks there as well). this commit only unblocks next monday's
release.
@rwaskiewicz rwaskiewicz mentioned this pull request Mar 3, 2022
14 tasks
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.

None yet

2 participants