jherr / no-bs-ts

No BS TS (Typescript) example code
257 stars 116 forks source link

Issues with conditionals.ts #5

Open tonisives opened 2 years ago

tonisives commented 2 years ago

I have installed node-fetch dependency. However, the conditionals.ts has an error

Argument of type 'unknown' is not assignable to parameter of type 'PokemonResults'.ts(2345)
Screenshot 2022-01-19 at 17 13 04

The data.json response from the fetch function returns Promise<unknown> type

Fix is to cast data to `Promise

fetch(url)
      .then((data) => data.json() as Promise<PokemonResults>)
      .then((data) => cb(data));

There is also error with fetch.

ts conditionals.ts
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/node_modules/node-fetch/src/index.js from /Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/conditionals.ts not supported.
Instead change the require of index.js in /Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/conditionals.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/conditionals.ts:15:38)
    at Module.m._compile (/Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/node_modules/ts-node/dist/index.js:735:29)
    at Object.require.extensions.<computed> [as .ts] (/Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/node_modules/ts-node/dist/index.js:737:16)
    at main (/Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/node_modules/ts-node/dist/bin.js:238:16)
    at Object.<anonymous> (/Users/tonis/workspace/web/ti-no-bs-ts/ts-basics/node_modules/ts-node/dist/bin.js:351:5) {
  code: 'ERR_REQUIRE_ESM'
}

The solution is to downgrade fetch to v2. link. yarn add node-fetch adds v3 automatically

Lam-Big-Water commented 3 months ago

Thank you so much!