#574

Electrobun doesn't work in TypeScript 7, causes deprecation warnings in 6

OpenRyanGarberopened 20d ago

baseUrl has been deprecated since TypeScript 6 and is removed outright in TypeScript 7. Extending .hutch/devkit/tsconfig.json (as the docs and examples all require) does not work at all and causes compilation errors in TypeScript 7. Overriding with "baseUrl": null and adding "paths": { "*": ".hutch/devkit/*" } lets Electrobun itself compile but overrides its own paths, breaking all electrobun/* imports in your Cottontail/Bun code.

Activity

1
RyanGarber16d ago

I've created this script to copy all of Electrobun's paths to my tsconfig.json (essentially doing what extends: would do) while trying to preserve whitespace as best as possible so diffs/formatters don't complain. This is not a long-term solution at all, but it works - proving Electrobun is not somehow incompatible with TS 6/7; it just hasn't been updated for some reason. Hopefully this saves the sanity of a few devs until this gets fixed properly.

#!/usr/bin/env node

import { readFile, writeFile } from "node:fs/promises";
import devkit from "../.hutch/devkit/tsconfig.json" with { type: "json" };

const original = await readFile(
   new URL("../tsconfig.json", import.meta.url),
   "utf-8",
);

const tab = /^(\s+)"compilerOptions"/gm.exec(original)?.[1];
if (!tab) {
   throw new Error("invalid tsconfig.json");
}

const pathsStartRegex = /^\s*"paths":\s*{\s*\n?/gm;
const pathsStartMatch = pathsStartRegex.exec(original);
if (!pathsStartMatch) {
   throw new Error("invalid tsconfig.json");
}
const pathsStart = pathsStartMatch.index;

const pathsEndRegex = /\n?\s*},?\s*$/gm;
if (!pathsEndRegex.exec(original)) {
   throw new Error("invalid tsconfig.json");
}
const pathsEnd = pathsEndRegex.lastIndex;

const paths = Object.entries(devkit.compilerOptions.paths).map(
   ([key, value]) =>
   	`${tab.repeat(3)}"${key}": [${value.map((target) => `"${target.replaceAll("./", "./.hutch/devkit/")}"`).join(",")}],`,
);

const updated =
   original.slice(0, pathsStart) +
   `${tab.repeat(2)}"paths": {\n${paths.join("\n")}\n${tab.repeat(2)}},` +
   original.slice(pathsEnd);

await writeFile(new URL("../tsconfig.json", import.meta.url), updated);