Search code examples
javascripttypescriptnpmumd

Confusing TypeScript errors due to hyphen in package name?


I've built a JavaScript/TypeScript library that I want to work as:

  • A global variable when called from either JavaScript and TypeScript
  • Accessed via RequireJS when called from either JavaScript and TypeScript
  • Has full unit test coverage

This is a UMD packaged using Webpack and deployed as an NPM package here (https://www.npmjs.com/~typed-contract). If you want the whole source of the branch I'm working on, you can find it here https://github.com/randarp/typed-contract/tree/feature/Issue_5.1.

My problem is that I can't get it to work in all scenarios and my feeling is that the hyphen in the package name (i.e. typed-contract) is the culprit. Some examples:

  • In TypeScript, \Code\TypedContract.Specs\Contract.TypeScript.requireJS.spec.ts
    • If I use import { contract } from "typed-contract" then my unit tests fail, but the WebStorm transpiler says it's correct.
    • If I use import { contract } from "typedcontract" then my unit tests pass, but the transpiler says Error:(4, 26) TS2306:File 'C:/Projects/TypedContract/Code/typedcontract.d.ts' is not a module.
  • I have an external NodeJS application to test this. In that case:
    • If I use import {contract} from 'typed-contract' it works as expected
    • If I use import {contract} from 'typedcontract' it won't load the application because it can't find the NPM package under node_modules.

I'm starting to think I need to deprecate this package and start a new one without a hyphen unless somebody can give some clues as to what may be going on because my "programming by permutation" isn't working.


Solution

  • Well, after a few more failed attempts, I created a new package and deprecated the old one. It can now be found at:

    https://www.npmjs.com/package/typedcontract.

    I just couldn't get it to work in all scenarios with the hyphen, so this seemed like the best long term solution.