Skip to content

Errors compiling runtime agnostic JavaScript to TypeScript #59439

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

Closed
guest271314 opened this issue Jul 27, 2024 · 10 comments
Closed

Errors compiling runtime agnostic JavaScript to TypeScript #59439

guest271314 opened this issue Jul 27, 2024 · 10 comments

Comments

@guest271314
Copy link

🔎 Search Terms

runtime agnostic

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because: Using npm:@typescript-deploys/pr-build@5.6.0-pr-58573-19

⏯ Playground Link

No response

💻 Code

/*
#!/usr/bin/env -S /home/user/bin/deno run -A /home/user/bin/nm_host.js
#!/usr/bin/env -S /home/user/bin/node --experimental-default-type=module /home/user/bin/nm_host.js
#!/usr/bin/env -S /home/user/bin/bun run /home/user/bin/nm_host.js
*/


const runtime = navigator.userAgent;
const buffer = new ArrayBuffer(0, { maxByteLength: 1024 ** 2 });
const view = new DataView(buffer);
const encoder = new TextEncoder();
const { dirname, filename, url } = import.meta;

let readable, writable, exit, args, cwd;

if (runtime.startsWith("Deno")) {
  ({ readable } = Deno.stdin);
  ({ writable } = Deno.stdout);
  ({ exit } = Deno);
  ({ args } = Deno);
}

if (runtime.startsWith("Node")) {
  const { Duplex } = await import("node:stream");
  ({ readable } = Duplex.toWeb(process.stdin));
  ({ writable } = Duplex.toWeb(process.stdout));
  ({ exit } = process);
  ({ argv: args } = process);
}

if (runtime.startsWith("Bun")) {
  readable = Bun.file("/dev/stdin").stream();
  writable = new WritableStream({
    async write(value) {
      await Bun.write(Bun.stdout, value);
    },
  }, new CountQueuingStrategy({ highWaterMark: Infinity }));
  ({ exit } = process);
  ({ argv: args } = Bun);
}

const hostdir = args.at(-2).slice(0, args.at(-2).lastIndexOf("/"));


if (runtime.startsWith("Bun") || runtime.startsWith("Node")) {
  process.chdir(hostdir);
  cwd = process.cwd();
}

if (runtime.startsWith("Deno")) {
  await Deno.chdir(hostdir);
  cwd = Deno.cwd();
}

function encodeMessage(message) {
  return encoder.encode(JSON.stringify(message));
}

async function* getMessage() {
  let messageLength = 0;
  let readOffset = 0;
  for await (let message of readable) {
    if (buffer.byteLength === 0) {
      buffer.resize(4);
      for (let i = 0; i < 4; i++) {
        view.setUint8(i, message[i]);
      }
      messageLength = view.getUint32(0, true);
      message = message.subarray(4);
      buffer.resize(0);
    }
    buffer.resize(buffer.byteLength + message.length);
    for (let i = 0; i < message.length; i++, readOffset++) {
      view.setUint8(readOffset, message[i]);
    }
    if (buffer.byteLength === messageLength) {
      yield new Uint8Array(buffer);
      messageLength = 0;
      readOffset = 0;
      buffer.resize(0);
    }
  }
}

async function sendMessage(message) {
  await new Blob([
    new Uint8Array(new Uint32Array([message.length]).buffer),
    message,
  ])
    .stream()
    .pipeTo(writable, { preventClose: true });
}

try {
  // await sendMessage(encodeMessage([{ dirname, filename, url }, { cwd }, ...args]));
  for await (const message of getMessage()) {
    await sendMessage(message);
  }
} catch (e) {
  exit();
}

/*
export {
  args,
  encodeMessage,
  exit,
  getMessage,
  readable,
  sendMessage,
  writable,
};
*/

🙁 Actual behavior

bun install npm:@typescript-deploys/pr-build@5.6.0-pr-58573-19
bun install undici-types
bun install @types/bun
bun install @types/node
mkdir -p node_modules/@types/deno
deno types > lib.deno.d.ts
cp lib.deno.d.ts node_modules/@types/deno

Rename nm_host.js to nm_host.ts.

./node_modules/.bin/tsc nm_host.ts --target esnext --allowJs --module esnext --moduleResolution bundler

A lot of errors. Summarized at the bottom of the attached file typescript-errors.txt

Errors  Files
    18  node_modules/@types/deno/lib.deno.d.ts:5
     1  node_modules/@types/node/buffer.d.ts:2273
     1  node_modules/@types/node/crypto.d.ts:4515
     2  node_modules/@types/node/dom-events.d.ts:108
     7  node_modules/@types/node/globals.d.ts:87
     2  node_modules/@types/node/module.d.ts:283
     4  node_modules/@types/node/perf_hooks.d.ts:836
     2  node_modules/@types/node/url.d.ts:933
     2  node_modules/@types/node/util.d.ts:1390
     3  node_modules/@types/node/worker_threads.d.ts:665
     1  node_modules/bun-types/bun.d.ts:117
    15  node_modules/bun-types/globals.d.ts:1103
     1  node_modules/bun-types/overrides.d.ts:3
     2  node_modules/bun-types/wasm.d.ts:1

🙂 Expected behavior

What I'm really trying to do here is convert valid, working runtime agnostic JavaScript that achieves the expected result using the same code with node, deno, and bun, to TypeScript, and create a .ts file from JavaScript source.

Additional information about the issue

Using npm:@typescript-deploys/pr-build@5.6.0-pr-58573-19 for resizable ArrayBuffer support.

@guest271314
Copy link
Author

typescript-errors.txt

@nmain
Copy link

nmain commented Jul 27, 2024

Looks like you're trying to use all of @types/node, @types/deno, and bun-types at the same time. That won't work; they describe much of the same common global functionality but with slightly different types. It's a nice idea, but someone will have to do the work first to build types for that. This repository only maintains the typescript compiler and types for the ecmascript environment and the html/web environment.

@guest271314
Copy link
Author

Looks like you're trying to use all of @types/node, @types/deno, and bun-types at the same time. That won't work;

Why won't this work?

If it doesn't, it should.

@MartinJohns
Copy link
Contributor

Why won't this work?

He wrote why it won't work, and the reason makes sense.

@guest271314
Copy link
Author

Doesn't make sense to me. If we can branch in JavaScript with basic if conditions and statements, we should be able to branch in TypeScript.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 27, 2024

For example one of your included types says the global type Event has a cancelBubble property that is of type boolean, other included types says this property is of type () => void. It can't be both at the same time. And the types do not say "if navigator.userAgent starts with "deno" the type is this and that", they describe types unconditionally. You can write types that are either A or B depending on another type (basically the if condition), but those included libraries don't do that.

This issue does not seem to be describe an actual bug in TypeScript and seems to be a question. Please note that this repository is for bug reports and feature requests, it's not a general purpose help forum (see this). You can try the TypeScript Discord or StackExchange for this (or any other).

Personally I would advice to first learn the basics of TypeScript using simpler projects, instead of forcefully trying to push your idea without understanding what you're doing.

@guest271314
Copy link
Author

@MartinJohns

Please note that this repository is for bug reports and feature requests,

Then I guess this is a bug and a feature request.

If the code works as intended in JavaScript the code should work as intended in TypeScript, if in fact TypeScript is a "superset" of JavaScript.

Personally I would advice to first learn the basics of TypeScript using simpler projects

I don't do simple projects. I test, experiment, hack multiple JavaScript engines and runtimes. The requirement must be challenging for me to even consider diving in to.

When possible, as in this case, I use the same code for node, deno, and bun.

instead of forcefully trying to push your idea without understanding what you're doing.

It's very simple. My goal is to create a .ts file containing the TypeScript programming language representation of working JavaScript code, using tsc.

My only interest here is to visually observe what TypeScript programming language outputs for the JavaScript code I have written that works as intended.

If TypeScript does not have the capability to branch in basic if conditions and statements, then I can adjust my requirement down to TypeScript programming language capabilities.

Does tsc have the capability to compile a Deno-only version to TypeScript from JavaScript?

@MartinJohns
Copy link
Contributor

Then I guess this is a bug and a feature request.

But it's not. You're neither demonstrating a bug with TypeScript, nor are you suggesting a specific feature (which has it's own issue template). You're facing an issue using TypeScript and don't understand the error message you get and ask here for help.

if in fact TypeScript is a "superset" of JavaScript.

Relevant link: Is TypeScript really a superset of JavaScript?

I don't do simple projects. I test, experiment, hack multiple JavaScript engines and runtimes. The requirement must be challenging for me to even consider diving in to.

I too enjoy the challenge of ramming my head through the door. I don't waste time learning to use door handles!

Learning the basics of whatever tool you choose to use is an appropriate first step to face whatever challenge you choose. By skipping this crucial step you end up in situations like this: Not understanding the error you get, trying to report it as a bug when it's clearly not.

If TypeScript does not have the capability to branch in basic if conditions and statements, then I can adjust my requirement down to TypeScript programming language capabilities.

As I mentioned earlier: TypeScript does have this capability, if the types are written accordingly. The type libraries you included are not written like that (intentionally). So you can either fall back to any, or write your own type definitions for whatever libraries you use.

Does tsc have the capability to compile a Deno-only version to TypeScript from JavaScript?

The TypeScript compiler tsc can not be used to compile to TypeScript code, no. That is not a supported use case.


Anyway, I'm out. This issue will lead to nowhere.

@guest271314
Copy link
Author

The TypeScript compiler tsc can not be used to compile to TypeScript code, no. That is not a supported use case.

That's all you had to say in the first place.

@guest271314
Copy link
Author

@nmain @MartinJohns I made it work https://github.com/guest271314/native-messaging-typescript.

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

3 participants