statelyai / xstate-tools

Public monorepo for XState tooling

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typegen with Deno

mfulton26 opened this issue · comments

XState VSCode's Typegen functionality does not currently work with TypeScript code when using Deno because Typegen does not insert the generated module's path using its extension. This is required when using Deno. From Modules | Manual | Deno:

Deno by default standardizes the way modules are imported in both JavaScript and TypeScript using the ECMAScript 6 import/export standard.

It adopts browser-like module resolution, meaning that file names must be specified in full. You may not omit the file extension…

I would like for VSCode to detect Deno usage and generate imports differently. e.g. By checking .vscode/settings.json and/or detecting that the Deno Language Server is being used for the current TypeScript file.

Current

/* demo.ts */
import { createMachine } from "npm:xstate@4.37.2";

createMachine({
  tsTypes: {} as import("./demo.typegen").Typegen0,
});

Desired

/* demo.ts */
import { createMachine } from "npm:xstate@4.37.2";

createMachine({
  tsTypes: {} as import("./demo.typegen.ts").Typegen0,
});

Diff

@@ -2,5 +2,5 @@
 import { createMachine } from "npm:xstate@4.37.2";
 
 createMachine({
-  tsTypes: {} as import("./demo.typegen").Typegen0,
+  tsTypes: {} as import("./demo.typegen.ts").Typegen0,
 });

In here we introduced useDeclarationFileForTypegenData setting that should help you here.

Nice. Thank you @Andarist. I can confirm that enabling xstate.useDeclarationFileForTypegenData works and creates a declaration file which works with Deno.

I was originally thinking of suggesting some Deno workspace detection logic (e.g. looking for "deno.enable": true in .vscode/settings.json, etc.) but now that I wonder if emitting a declaration file for typegen could be made teh default behavior as it works more universally.

That way anyone using Node, Bun, Deno, or some other environment will be able to use typegen without needing to change default settings.

but now that I wonder if emitting a declaration file for typegen could be made teh default behavior as it works more universally.

That's the plan :) I'll take another look at the state of things before doing this because I need to confirm that declaration files can be emitted from projects using this. I have a vague recollection of Andrew Branch from the TS team mentioning that this pattern might sometimes not work for that but I don't recall the details