rikukissa / typehole

TypeScript development tool for Visual Studio Code that helps you automate creating the initial static typing for runtime values

Home Page:https://marketplace.visualstudio.com/items?itemName=rikurouvila.typehole

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ESM support

d7sd6u opened this issue · comments

commented

Hi! Unfortunately typehole package is bundled as CommonJS module and doesn't work with true ESM projects... Or at least it doesn't work with my setup: Node.JS v16.18.1, ts-node v10.9.1.

The error is:

TypeError: typehole.t is not a function

My guess is, when importing CJS module with Proxy as it's default export from ESM module, Proxy "disappears" and POJO is what remains of it. Maybe it's because of Typescript compiler or Node.JS compatibility mode or something like that, I am not an expert in Node.JS's ECMAScript module loader behavior.

Minimal reproducible example consists of three files, index.ts, package.json and tsconfig.json:

import tp from "typehole";

// sadly, it is impossible to use typehole directly with
// --noPropertyAccessFromIndexSignature and --noUncheckedIndexedAccess
const typehole = tp as any;

const unknown: any = { testProperty: 123 };

const value = typehole.t(unknown);

console.log(value);
{
  "main": "index.ts",
  "type": "module",
  "scripts": {
    "start": "ts-node-esm index.ts",
  },
  "dependencies": {
    "@types/node": "^18.13.0",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.5"
  },
  "devDependencies": {
    "@tsconfig/node-lts-strictest-esm": "^18.12.1",
    "typehole": "^1.7.0"
  }
}
{
  "extends": "@tsconfig/node-lts-strictest-esm/tsconfig.json",
  "include": ["**/*.ts"]
}

I suggest tweaking bundler to output ESM bundle as well and adding something like this to package.json:

  "exports": {
    "require": "./dist/index.js",
    "import": "./esm/index.js"
  },
  "type": "module",

Hey! Thank you very much for reporting this. I'll have a look as soon as I can. Meanwhile, feel free to submit a pull request if you've already tested this approach and found that it works