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

(new CustomError) instanceof CustomError => false #833

Closed
ken0x0a opened this issue May 15, 2019 · 6 comments
Closed

(new CustomError) instanceof CustomError => false #833

ken0x0a opened this issue May 15, 2019 · 6 comments

Comments

@ken0x0a
Copy link

ken0x0a commented May 15, 2019

Maybe related to #583

(new CustomError) instanceof CustomError
// => false 
// should be `true` but will get `false` @ ts-node
// `true` @ node

In addition, constructor also works unexpectedly.

(new WithoutConstructorError).constructor === WithoutConstructorError // => false
(new WithoutConstructorError).constructor === Error // => true

I've tested using following code.
Adding constructor in class did not change result.

class WithoutConstructorError extends Error {}

(new Error).name
(new WithoutConstructorError).name

(new Error) instanceof Error
(new Error) instanceof WithoutConstructorError
(new WithoutConstructorError) instanceof WithoutConstructorError
(new WithoutConstructorError) instanceof Error

(new Error).constructor === Error
(new Error).constructor === WithoutConstructorError
(new WithoutConstructorError).constructor === WithoutConstructorError
(new WithoutConstructorError).constructor === Error
@blakeembrey
Copy link
Member

I think this may be related to your target ES version, but I’ll investigate more when I have a chance. Are you specifying a custom target at all?

@iam4x
Copy link

iam4x commented May 24, 2019

I was having a similar issue, narrowed it down to nodejs itself! Might be relevant nodejs/node#27859 ?

@ken0x0a
Copy link
Author

ken0x0a commented May 25, 2019

@blakeembrey Thank you for your reply!

I'm sorry I didn't notice.

Actually I didn't understand what you mean, but I tried target ES versions (es5, es2015, es2018), and got same result.

With following configuration

    "target": "es5"
    "module": "commonjs"
> class WithoutConstructorError extends Error {}
'use strict'
>
undefined
> (new Error).name
'Error'
> (new WithoutConstructorError).name
'Error'
>

With following configuration also same result

    "target": "es2015" or "es2018"
    "module": "commonjs"

@ken0x0a
Copy link
Author

ken0x0a commented May 25, 2019

@iam4x Thank you for your reply!

Since I got expected result @ node, I guess that is not the something.

@davoam
Copy link

davoam commented Dec 11, 2019

I think this may be related to your target ES version, but I’ll investigate more when I have a chance. Are you specifying a custom target at all?

In my case changing target from es5 to es2018 helped.

Also take a look at this issue microsoft/TypeScript#10166

@blakeembrey
Copy link
Member

Thanks @davoam, I'm going to close this issue since it's most likely related to emitted output. It's a reason I've always used something like https://www.npmjs.com/package/make-error in custom projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants