JacobZwang
JacobZwang7mo ago

How to import enums from npm:typescript package?

The following code crashes because the enum is undefined at runtime.
import * as ts from "npm:typescript";
console.log(ts.ScriptTarget.ESNext);
import * as ts from "npm:typescript";
console.log(ts.ScriptTarget.ESNext);
error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'ESNext')
target: ts.ScriptTarget.ESNext,
error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'ESNext')
target: ts.ScriptTarget.ESNext,
I think this issue is related, but I can't figure out how to actually solve this. https://github.com/denoland/deno/issues/7605 I tried running it in Bun and it worked as expected and I can't find any compiler options in deno that seem related. How should I fix this?
GitHub
const enum error clarification · Issue #7605 · denoland/deno
This error appears after I switched to Deno 1.4.1 and fixed all the import type errors. Skillz@Home Discordeno % deno run --allow-net --allow-read --unstable debug.ts error: Uncaught ReferenceError...
4 Replies
marvinh.
marvinh.7mo ago
- import * as ts from "npm:typescript";
+ import ts from "npm:typescript";
- import * as ts from "npm:typescript";
+ import ts from "npm:typescript";
JacobZwang
JacobZwangOP7mo ago
oh huh, why does this work? and why does this also not work?
import { ScriptTarget } from "npm:typescript";
console.log(ScriptTarget.ESNext);
import { ScriptTarget } from "npm:typescript";
console.log(ScriptTarget.ESNext);
marvinh.
marvinh.7mo ago
Because they don't export named variables, they export a namespace in their package:
namespace ts {
// ...
}

export = ts
namespace ts {
// ...
}

export = ts
JacobZwang
JacobZwangOP7mo ago
ah, I see. thanks!