dice-roller / rpg-dice-roller

An advanced JS based dice roller that can roll various types of dice and modifiers, along with mathematical equations.
https://dice-roller.github.io/documentation
MIT License
230 stars 58 forks source link

Issues with module's types #247

Open mmerfort opened 2 years ago

mmerfort commented 2 years ago

Describe the bug Some of the type definitions seems to be faulty or incomplete.

To Reproduce Steps to reproduce the behaviour:

  1. Bootstrap a new typescript project with the defaults (npm i typescript --save-dev && npx tsc --init)
  2. Install the module via npm (npm install @dice-roller/rpg-dice-roller)
  3. Create an example file index.ts (see below)
  4. Set skipLibCheck to false in tsconfig.json
  5. Run tsc to build the project

Expected behaviour No type errors show up.

Code snippet

import { DiceRoll } from "@dice-roller/rpg-dice-roller"

const roller = new DiceRoll("6d6")

console.log(roller.toString())

Environment:

GreenImp commented 2 years ago

I don't use Typescript myself (Although I really want to, I haven't found the time to look into it). I've tried following your instructions, but running

./node_modules/typescript/bin/tsc

Throws the following:

error TS18003: No inputs were found in config file '/var/www/projects/typescript-test/tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.

Found 1 error.

I'm assuming this isn't the issue you're referring to? Are you able to provide the error that you're seeing please?

The type definitions are all automatically generated from the doc blocks in the code. I'm happy for a PR to fix any faulty ones.

mmerfort commented 2 years ago

Sorry, I forgot to attach the actual error. Here it is:

$ npx tsc
node_modules/@dice-roller/rpg-dice-roller/types/dice/FudgeDice.d.ts:18:104 - error TS2304: Cannot find name 'Modifier'.

18     constructor(nonBlanks?: number | undefined, qty?: number | undefined, modifiers?: {} | Map<string, Modifier> | Modifier[] | null | undefined);
                                                                                                          ~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/dice/FudgeDice.d.ts:18:116 - error TS2304: Cannot find name 'Modifier'.

18     constructor(nonBlanks?: number | undefined, qty?: number | undefined, modifiers?: {} | Map<string, Modifier> | Modifier[] | null | undefined);
                                                                                                                      ~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/dice/PercentileDice.d.ts:17:72 - error TS2304: Cannot find name 'Modifier'.

17     constructor(qty?: number | undefined, modifiers?: {} | Map<string, Modifier> | Modifier[] | null | undefined, sidesAsNumber?: boolean | undefined);
                                                                          ~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/dice/PercentileDice.d.ts:17:84 - error TS2304: Cannot find name 'Modifier'.

17     constructor(qty?: number | undefined, modifiers?: {} | Map<string, Modifier> | Modifier[] | null | undefined, sidesAsNumber?: boolean | undefined);
                                                                                      ~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/exceptions/DieActionValueError.d.ts:12:22 - error TS2304: Cannot find name 'StandardDice'.

12     constructor(die: StandardDice, action?: string | null | undefined);
                        ~~~~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/exceptions/DieActionValueError.d.ts:14:10 - error TS2304: Cannot find name 'StandardDice'.

14     die: StandardDice;
            ~~~~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/modifiers/CriticalSuccessModifier.d.ts:19:31 - error TS2304: Cannot find name 'ComparePoint'.

19     constructor(comparePoint: ComparePoint);
                                 ~~~~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/modifiers/Modifier.d.ts:39:18 - error TS2304: Cannot find name 'RollResults'.

39     run(results: RollResults, _context: StandardDice | RollGroup): RollResults;
                    ~~~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/modifiers/Modifier.d.ts:39:41 - error TS2304: Cannot find name 'StandardDice'.

39     run(results: RollResults, _context: StandardDice | RollGroup): RollResults;
                                           ~~~~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/modifiers/Modifier.d.ts:39:56 - error TS2304: Cannot find name 'RollGroup'.

39     run(results: RollResults, _context: StandardDice | RollGroup): RollResults;
                                                          ~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/modifiers/Modifier.d.ts:39:68 - error TS2304: Cannot find name 'RollResults'.

39     run(results: RollResults, _context: StandardDice | RollGroup): RollResults;
                                                                      ~~~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/modifiers/SortingModifier.d.ts:51:41 - error TS2304: Cannot find name 'StandardDice'.

51     run(results: RollResults, _context: StandardDice | RollGroup): RollResults;
                                           ~~~~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/modifiers/SortingModifier.d.ts:51:56 - error TS2304: Cannot find name 'RollGroup'.

51     run(results: RollResults, _context: StandardDice | RollGroup): RollResults;
                                                          ~~~~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/utilities/NumberGenerator.d.ts:26:20 - error TS2304: Cannot find name 'Engine'.

26     browserCrypto: Engine;
                      ~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/utilities/NumberGenerator.d.ts:27:17 - error TS2304: Cannot find name 'Engine'.

27     nodeCrypto: Engine;
                   ~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/utilities/NumberGenerator.d.ts:29:17 - error TS2304: Cannot find name 'Engine'.

29     nativeMath: Engine;
                   ~~~~~~

node_modules/@dice-roller/rpg-dice-roller/types/utilities/NumberGenerator.d.ts:64:26 - error TS2304: Cannot find name 'Engine'.

64     constructor(engine?: Engine | {
                            ~~~~~~

Found 17 errors in 7 files.

Errors  Files
     2  node_modules/@dice-roller/rpg-dice-roller/types/dice/FudgeDice.d.ts:18
     2  node_modules/@dice-roller/rpg-dice-roller/types/dice/PercentileDice.d.ts:17
     2  node_modules/@dice-roller/rpg-dice-roller/types/exceptions/DieActionValueError.d.ts:12
     1  node_modules/@dice-roller/rpg-dice-roller/types/modifiers/CriticalSuccessModifier.d.ts:19
     4  node_modules/@dice-roller/rpg-dice-roller/types/modifiers/Modifier.d.ts:39
     2  node_modules/@dice-roller/rpg-dice-roller/types/modifiers/SortingModifier.d.ts:51
     4  node_modules/@dice-roller/rpg-dice-roller/types/utilities/NumberGenerator.d.ts:26

I also forgot to mention that the test file is just called test.ts which then gets transpiled to test.js. The generated code is still valid so the errors aren't critical.

Output after executing tsc:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rpg_dice_roller_1 = require("@dice-roller/rpg-dice-roller");
const roller = new rpg_dice_roller_1.DiceRoll("6d6");
console.log(roller.toString());
JamesSkemp commented 2 years ago

I've submitted a PR with partial fixes for this.

There may be a better way to handle this, but the problem is the seven files above reference types in the JS docs, but when the TypeScript definitions are generated they're not getting resolved/imported.

Two files still have issues:

  1. node_modules/@dice-roller/rpg-dice-roller/types/exceptions/DieActionValueError.d.ts
  2. node_modules/@dice-roller/rpg-dice-roller/types/modifiers/Modifier.d.ts

For DieActionValueError StandardDice is referenced in the docs, but not imported.

For Modifier RollGroup, RollResults, and StandardDice are referenced in the docs, but are unknown.

For both of these ESLint errors on a detected dependency cycle.

JamesSkemp commented 1 year ago

Well drat. I just started a new project that uses this and just ran into these issues again ... :|