alpaca-ts icon indicating copy to clipboard operation
alpaca-ts copied to clipboard

Does not compile with TypeScript 4.7 module = nodenext

Open vitalets opened this issue 2 years ago • 26 comments

Description Can't compile TypeScript project using @master-chief/alpaca with module = nodenext in tsconfig.json.

Expected TypeScript compilation pass.

Reproduction

  1. Install latest TypeScript (4.7.3)
  2. Install latest @master-chief/alpaca (6.3.20): npm i @master-chief/alpaca
  3. Create tsconfig with module=NodeNext:
    "compilerOptions": {
      "target": "es2020",
      "module": "NodeNext",
      "strict": true,
      "allowSyntheticDefaultImports": true,
      "esModuleInterop": true
    },
    
  4. Create index.ts importing alpaca stuff:
    import { AlpacaClient, AlpacaStream } from '@master-chief/alpaca';
    
  5. Try to compile TypeScript: tsc

Logs

error TS7016: Could not find a declaration file for module '@master-chief/alpaca'. '.../node_modules/@master-chief/alpaca/dist/mjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/master-chief__alpaca` if it exists or add a new declaration (.d.ts) file containing `declare module '@master-chief/alpaca';`

vitalets avatar Jun 14 '22 07:06 vitalets

Will investigate.

117 avatar Jun 14 '22 13:06 117

I have the same problem. A hotfix or a workaround, would be really appreciated! 😄

WinstonNau avatar Jun 19 '22 16:06 WinstonNau

I have the same problem. A hotfix or a workaround, would be really appreciated! 😄

Error: grafik

WinstonNau avatar Jun 19 '22 16:06 WinstonNau

been busy 😢 ill try today

117 avatar Jun 19 '22 16:06 117

well a temporary workaround would be

// @ts-ignore
import { AlpacaClient } from "@master-chief/alpaca";

and im able to compile with

{
  "compilerOptions": {
    "target": "es2020",
    "module": "NodeNext",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

117 avatar Jun 19 '22 16:06 117

You can also do..

{
  "compilerOptions": {
    ...
    "noImplicitAny": false,
  }
}

117 avatar Jun 19 '22 16:06 117

I don't know why its unable to find the typings, something to do with the way typescript ^4.8.0-dev.20220619 handles resolution? If I may ask why are you using the nightly version of typescript anyways? It is designated as unstable.

117 avatar Jun 19 '22 16:06 117

Hmm trying one more thing.

117 avatar Jun 19 '22 17:06 117

Yeah idk. Works fine on every non-dev typescript version for me.

117 avatar Jun 19 '22 17:06 117

Yeah idk. Works fine on every non-dev typescript version for me.

Hmmm interesting. I was using TypeScript version 4.7.3 there and got the error. I tried installing the 4.6.3 version and still got the same error.

"compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve"
  },

WinstonNau avatar Jun 20 '22 09:06 WinstonNau

@WinstonNau ill try

117 avatar Jun 20 '22 17:06 117

@117 Maybe the reason is that dist/cjs/*.cjs files are looking for typings in @types/*.d.cts files (not in @types/*.d.ts)? One idea is instead of renaming dist/cjs/*.js -> dist/cjs/*.cjs generate simple package.json in dist/cjs:

{"type":"commonjs"}

vitalets avatar Jun 21 '22 16:06 vitalets

will try this!

117 avatar Jun 22 '22 08:06 117

@WinstonNau ill try

Thank you! Let me know if you might have a fix for this problem :)

WinstonNau avatar Jun 22 '22 23:06 WinstonNau

oh my god .d.cts works

117 avatar Jun 23 '22 03:06 117

i'm going to write a new build script and config which generates the new declarations tomorrow. too tired right now

117 avatar Jun 23 '22 03:06 117

delaying another day 😞 work is kicking my ass

117 avatar Jun 24 '22 02:06 117

Ok! Before I publish it please test and let me know if this solves it for you. Try npm i 117/alpaca and let me know :)

  "dependencies": {
    "@master-chief/alpaca": "github:117/alpaca"
  },

I have tested this personally with the following tsconfig.

{
  "compilerOptions": {
    "target": "es2020",
    "module": "NodeNext",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

@WinstonNau @vitalets

117 avatar Jun 24 '22 21:06 117

@117 does not compile for me :(

npx tsc

node_modules/@master-chief/alpaca/dist/cjs/index.d.cts:10:353 - error TS2307: Cannot find module './entities' or its corresponding type declarations.

10 export { Account, Order, OrderCancelation, Position, Asset, Watchlist, Calendar, Clock, DefaultCredentials, OAuthCredentials, AccountConfigurations, NonTradeActivity, TradeActivity, Activity, PortfolioHistory, Bar, Bar_v1, Quote, LastQuote_v1, LastTrade_v1, Trade, PageOfBars, PageOfQuotes, PageOfTrades, Snapshot, DataSource, Channel, Message, } from './entities';
                                                                                                                                                                                                                                                                                                                                                                   ~~~~~~~~~~~~

node_modules/@master-chief/alpaca/dist/cjs/index.d.cts:11:449 - error TS2307: Cannot find module './params' or its corresponding type declarations.

11 export { GetOrder, GetOrders, PlaceOrder, ReplaceOrder, CancelOrder, GetPosition, ClosePosition, GetAsset, GetAssets, GetWatchList, CreateWatchList, UpdateWatchList, AddToWatchList, RemoveFromWatchList, DeleteWatchList, GetCalendar, UpdateAccountConfigurations, GetAccountActivities, GetPortfolioHistory, GetBars, GetBars_v1, GetTrades, GetQuotes, GetSnapshot, GetSnapshots, GetLastTrade_v1, GetLastQuote_v1, BarsV1Timeframe, BarsTimeframe, } from './params';
                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ~~~~~~~~~~

node_modules/@master-chief/alpaca/dist/cjs/stream.d.cts:1:23 - error TS2688: Cannot find type definition file for 'ws'.

1 /// <reference types="ws" />
                        ~~

node_modules/isomorphic-ws/index.d.ts:6:28 - error TS7016: Could not find a declaration file for module 'ws'. '/Users/vitalets/projects/tmp/node_modules/ws/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/ws` if it exists or add a new declaration (.d.ts) file containing `declare module 'ws';`

6 import WebSocket = require('ws')
                             ~~~~


Found 4 errors in 3 files.

Errors  Files
     2  node_modules/@master-chief/alpaca/dist/cjs/index.d.cts:10
     1  node_modules/@master-chief/alpaca/dist/cjs/stream.d.cts:1
     1  node_modules/isomorphic-ws/index.d.ts:6

Environment: npm ls

[email protected] /projects/tmp
├── @master-chief/[email protected] (git+ssh://[email protected]/117/alpaca.git#79a216be9073b01587b2bc599919275bbaa28797)
└── [email protected]

cat tsconfig.json

{
  "compilerOptions": {
    "target": "es2020",
    "module": "NodeNext",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "noEmit": true,
  },
  "include": [ "src" ]
}

node -v

v16.14.0

vitalets avatar Jun 25 '22 08:06 vitalets

I get this error in WebStorm

grafik

and this error after starting the app:

grafik

and this is my tsconfig (the module automatically changes to esnext so I can't use NodeNext)

grafik

WinstonNau avatar Jun 25 '22 16:06 WinstonNau

Hi @117! Any update on this issue? =)

vitalets avatar Jul 14 '22 08:07 vitalets

no update ):

117 avatar Jul 28 '22 15:07 117

v7 is on the way, you can track it here #113

117 avatar Apr 06 '23 18:04 117

I also received error TS7016 when trying to use @master-chief/alpaca v6.3.20 in a TS project using ESM with moduleResolution set to node16 and module set to es2022. Upgrading to @master-chief/alpaca-ts v7.0.9-alpha solved the issue for me! 👍

bennycode avatar Aug 06 '23 12:08 bennycode

@117 I also tried "@master-chief/alpaca": "github:117/alpaca". It works when I set "skipLibCheck" to true in my TS config. Given that this library is currently the best solution for TS-ESM projects (as it is executable and provides typings), can you make a release from it?

bennycode avatar Aug 07 '23 16:08 bennycode

Hey @117, today I noticed that the error callback of stream.ts is typed wrongly: https://github.com/alpacahq/alpaca-ts/blob/d4a2b7bae909f8c4e4d80752940c1a9cd60e7617/src/stream.ts#L24

Instead of a WebSocket.ErrorEvent a custom error in the format of { T: 'error', code: 406, msg: 'connection limit exceeded' } is returned. I made a PR for it: https://github.com/alpacahq/alpaca-ts/pull/118

bennycode avatar Oct 03 '23 11:10 bennycode