swellstores / swell-js

JS library for building storefronts and checkouts with Swell ecommerce.
https://swell.is
MIT License
138 stars 30 forks source link

Typescript Issue in 4.2.4 No Default Export #195

Open jeff-wood-readyup opened 1 day ago

jeff-wood-readyup commented 1 day ago

I am having an issue with Typescript, it is saying the swell-js library does not have a default export and so it is not able to pull in the type definitions correctly, resulting in it defaulting to an any type.

Screenshot 2024-11-14 at 11 35 59 PM Screenshot 2024-11-14 at 11 36 10 PM Screenshot 2024-11-14 at 11 36 19 PM

My package.json dependencies:

"dependencies": {
    "@quasar/extras": "^1.16.4",
    "axios": "^1.2.1",
    "pinia": "^2.0.11",
    "quasar": "^2.16.0",
    "swell-js": "^4.2.2",
    "vue": "^3.4.18",
    "vue-router": "^4.0.12"
  },
  "devDependencies": {
    "@quasar/app-vite": "^2.0.0-beta.12",
    "@types/node": "^22.9.0",
    "@typescript-eslint/eslint-plugin": "^8.14.0",
    "@typescript-eslint/parser": "^8.14.0",
    "autoprefixer": "^10.4.2",
    "dotenv": "^16.4.5",
    "eslint": "^8.57.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-import": "^2.29.1",
    "eslint-plugin-vue": "^9.0.0",
    "prettier": "^3.0.3",
    "prettier-eslint": "^16.3.0",
    "typescript": "~5.5.3",
    "vite-plugin-checker": "^0.8.0",
    "vue-tsc": "^2.0.29"
  },

Any help resolving this would be greatly appreciated, my code itself works fine but the type errors are very annoying to deal with. Thanks!

awwit commented 1 day ago

@jeff-wood-readyup what if you try to import it this way:

import * as swell from 'swell-js';
ericingram commented 1 day ago

@awwit we should fix if true

jeff-wood-readyup commented 1 day ago

@jeff-wood-readyup what if you try to import it this way:

import * as swell from 'swell-js';

This was actually one of the first things I tried and it does remove the typescript error but it stops my code from actually working.

Screenshot 2024-11-15 at 10 11 35 AM

Screenshot 2024-11-15 at 10 11 23 AM

It seems that this method is importing the namespace for the types instead of actually importing the library.

jeff-wood-readyup commented 1 day ago

Doing a little poking around myself I added this to the end of the index.d.ts file and it seems to have solved my problem.

declare const swell: {
  version: string;
  init: typeof init;
  account: typeof account;
  attributes: typeof attributes;
  card: typeof card;
  cart: typeof cart;
  categories: typeof categories;
  content: typeof content;
  currency: typeof currency;
  locale: typeof locale;
  payment: typeof payment;
  products: typeof products;
  settings: typeof settings;
  subscriptions: typeof subscriptions;
  invoices: typeof invoices;
  session: typeof session;
  functions: typeof functions;
  utils: typeof utils;
  auth: typeof init;
  request: typeof request;
  get: typeof get;
  put: typeof put;
  post: typeof post;
  delete: typeof _delete;
};

export default swell;

I have forked the library for now, happy to submit a PR but I have only started using Swell recently so I am not sure if this code would work for all the functionality of the library.

jeff-wood-readyup commented 1 day ago

PR with fix is here - https://github.com/swellstores/swell-js/pull/197