Error when calling instanceof on the instance of the class extending the Error class
See original GitHub issueParcel Version: 1.12.4
Code sample
class CustomError extends Error {
name = "CustomError";
}
try {
const error = new CustomError("Error happened");
console.log("TRY: error", error.constructor.name, JSON.stringify(error));
console.log("TRY: instanceof CustomError", error instanceof CustomError);
console.log("TRY: instanceof Error", error instanceof Error);
throw error;
} catch (error) {
console.log("CATCH: error", error.constructor.name, JSON.stringify(error));
console.log("CATCH: instanceof CustomError", error instanceof CustomError);
console.log("CATCH: instanceof Error", error instanceof Error);
}
Respositories
- [Parcel] https://codesandbox.io/s/errorparcel-y33n8
- [Webpack] https://codesandbox.io/s/errorwebpack-xucu4
Output
TRY: error Error {"name":"CustomError"}
TRY: instanceof CustomError false
TRY: instanceof Error true
CATCH: error Error {"name":"CustomError"}
CATCH: instanceof CustomError false
CATCH: instanceof Error true
Expected output
TRY: error CustomError {"name":"CustomError"}
TRY: instanceof CustomError true
TRY: instanceof Error true
CATCH: error CustomError {"name":"CustomError"}
CATCH: instanceof CustomError true
CATCH: instanceof Error true
Description
I’ve switched my project from using Webpack to Parcel and suddenly some of the code stopped working. Parcel has problem with properly detecting class of an instance of the object that is extending built in class, like for example the Error
class. It works correctly with custom classes but fails with built in ones. This bug is not present in Webpack. After comparing transpiled code I can see that Webpack is using @babel/runtime/helpers/possibleConstructorReturn
helper function to initialize instance of the extended class. Is it something that can be easily fixed?
It also doesn’t work when I provide custom Babel config with the typescript
transformer.
EDIT:
I’ve just found this issue: https://github.com/parcel-bundler/parcel/issues/2023
Which looks like it would fix my problem. But I see that it’s only fixed in Parcel 2. Are you planning to fix that in Parcel 1? Or is there an easy hack to allow that?
Also a solution using the parcel-plugin-babel-typescript
plugin doesn’t work
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Yes. I guess the pipelining aspect could be documented better. The root issue here is that typescript’s approach to transpiling classes doesn’t really work with builtins. I’ll make sure this is done automatically in Parcel 2.
@mischnic Thanks!