RIAEvangelist / node-dominos-pizza-api

This is a node.js wrapper for the dominos pizza apis
MIT License
547 stars 132 forks source link

TypeScript Definitions file #109

Closed RIAEvangelist closed 8 months ago

RIAEvangelist commented 3 years ago

I would like to support typescript users, but I need a types.d file for the module to do so easily for Jr devs. I would appreciate it if someone would contribute this file to the repository.

Thank you. 🍕 🥳 🎉

mattcroat commented 3 years ago

I'm working on creating the ambient type declarations. :wave:

Type Definitions

item

item-missing-type

item-options

item-options-description

store-options

payment

promise

international-options

base64-options

store-menu-options

How Do We Use It?

Types that aren't part of a library, are ambient type declarations. Hence the d.ts extension. There's two options when it comes to using them:

npm i -D @types/node @types/dominos

The Node types are what a TypeScript user would have to install anyhow — and some of them are used in @types/dominos.

Usage With TypeScript

Since the library is using native JavaScript modules — this poses a problem for TypeScript when using things like nodemon, and ts-node in tandem. I propose this be included in the readme. The problem stems from when [ts-node fails when ES Modules are in the dependency graph in Node.js](). These are the steps to get it working:

  1. In your package.json include "type": "module"
  2. Your tsconfig.json should have:
    {
    "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "Node"
    }
    }
  3. You have to add a nodemon.json config (this is what resolves the issue):
    {
    "execMap": {
    "ts": "node --loader ts-node/esm"
    }
    }

Feedback

I would prefer if someone more experienced using the library could try out the types first (TypeScript knowledge is not required):

  1. Get the type definition file.
  2. npm init -y && npm i dominos && npm i -D @types/node inside a folder (TypeScript language server is part of VS Code so you don't have to install it here).
  3. Place the index.d.ts file at the root of the project, so it gets picked up.
  4. Create a index.ts file (the name is irrelevant, just make sure it has a .ts extension).
  5. You don't have to run the actual code, just observe the feedback from your editor (you can press Ctrl + Spacebar in VS Code to bring up autocomplete if you lose it).
  6. Report anything unexpected.