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

[BUG] Custom error imports are erased at compilation causing runtime errors #47

Closed
FredTreg opened this issue Nov 13, 2023 · 1 comment

Comments

@FredTreg
Copy link
Contributor

FredTreg commented Nov 13, 2023

Describe the bug
When using the construct Assert<type, ThrowError<MyInputError>> the import { MyInputError } from 'somewhere' statement gets removed at compilation time.

Generated code will fail with MyInputError is not defined at runtime.

Playground link
Compare this playground and this playground

In the first playground, you can see the import being removed, in the second playground it remains because the InputError class is used directly at line 4: this is the current workaround I found to make it work.

Expected behavior
One can use custom error classes when using Assert without having to create fake statements.

Additional context

  • ts-runtime-checks: 0.4.1
  • typescript: 5.2.2 (ESM)
GoogleFeud added a commit that referenced this issue Nov 21, 2023
GoogleFeud added a commit that referenced this issue Dec 23, 2023
@GoogleFeud
Copy link
Owner

This issue turned out to be more difficult than I thought, it will be resolved, but maybe in the next patch. There are two ways to fix this error:

Turn on the verbatimModuleSyntax typescript options (5.0 and up, only works with CommonJS)

Or using require directly:

import { MyError } = require("./path");

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

No branches or pull requests

2 participants