nats-io / nats.js

Apache License 2.0
53 stars 6 forks source link

ts(2371) compile error when importing library into angular 17 project #42

Closed mrosm20 closed 2 months ago

mrosm20 commented 2 months ago

Observed behavior

  1. install application using this command because i needed websockets: npx jsr add @nats-io/nats-core
  2. module import wasn't resolving: import * as Nats from "@nats-io/nats-core";
  3. changed: "moduleResolution": "node" to "moduleResolution": "bundler",
  4. everything seemed to resolved.
  5. compiled application and received this error: Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:25:21 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

    25 delete(k: string, match = Match.Exact): void;

Expected behavior

N/A

Server and client version

latest

Host environment

linux

Steps to reproduce

N/A

aricart commented 2 months ago

What is your runtime?

aricart commented 2 months ago

If you are running on node, (I see node_modules) remove the lib, and simply do npm add nats-io/nats-core the jsr libraries are ESM only, and possibly node doesn't like it.

mrosm20 commented 2 months ago

its running in angular which is a front end ts/js framwework, so I need web sockets which is not part the "npm install nats-io/nats-core" package. Only part of the ESM package "@jsr/nats-io__nats-core" Which when i install it give me these errors:

uild at: 2024-08-08T14:50:15.637Z - Hash: 7fbb08a184838f72 - Time: 3752ms

Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:18:23 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

18 findKeys(k: string, match = Match.Exact): string[];


Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:19:18 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

19   get(k: string, match = Match.Exact): string;

Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:20:19 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

20 last(k: string, match = Match.Exact): string;


Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:21:18 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

21   has(k: string, match = Match.Exact): boolean;

Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:22:29 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

22 set(k: string, v: string, match = Match.Exact): void;


Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:23:32 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

23   append(k: string, v: string, match = Match.Exact): void;

Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:24:21 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

24 values(k: string, match = Match.Exact): string[];


Error: node_modules/@nats-io/nats-core/_dist/src/headers.d.ts:25:21 - error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

25   delete(k: string, match = Match.Exact): void;

Angular Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/

mrosm20 commented 2 months ago

running typescript 5.2.2 node v20.15.1 typescript configuration

{ "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "downlevelIteration": true, "experimentalDecorators": true, "module": "es2020", "moduleResolution": "bundler", "importHelpers": true, "target": "ES2022", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2018", "dom" ], "resolveJsonModule": true, "esModuleInterop": true }, "angularCompilerOptions": { "fullTemplateTypeCheck": true, "strictInjectionParameters": true } }

aricart commented 2 months ago

Verified, if you import npm add nats-io/nats-core tsc won't complain about typescript definitions (as for that platform they are generated by tsc itself). - if you are using tsc, use the node libs.

aricart commented 2 months ago

For completeness:

package.json

{
  "type": "module",
  "dependencies": {
    "@nats-io/nats-core": "^3.0.0-24",
    "typescript": "^5.5.4"
  },
  "devDependencies": {
    "esbuild": "0.23.0"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "nodenext",
    "outDir": "./lib/",
    "moduleResolution": "nodenext",
    "sourceMap": true,
    "declaration": true,
    "allowJs": true,
    "removeComments": false,
    "resolveJsonModule": true
  },
  "include": [
    "./src/**/*"
  ]
}

test.ts

import {wsconnect} from "@nats-io/nats-core";

(async () => {
    const nc = await wsconnect({servers: "wss://demo.nats.io:8443"});
    const sub = nc.subscribe("foo");
    (async () => {
        for await(const m of sub) {
            console.log(m.subject);
        }
    })();

    setInterval(() => {
        nc.publish("foo");
    }, 1000);
})();
aricart@mac-studio /p/t/web [SIGINT]> tsc
aricart@mac-studio /p/t/web> esbuild lib/test.js --bundle --outfile=out.js --tree-shaking=true --minify

  out.js  146.2kb

⚡ Done in 17ms
aricart@mac-studio /p/t/web> deno run -A out.js
foo
foo
foo
^C⏎