microsoft / microsoft/DefinitelyTyped-tools

Declarations for a global library incorrectly interpreted as module

Open
#1,192 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
423
Forks
237
Avg merge
18h 18m
Merged PRs (30d)
11

Description

I'm trying to add some types to the DefinitelyTyped repo for a very small, quite old JS library, which uses a simple global. I used dts-gen to create the project after cloning the DT repo, and I believe the structure and setup is correct.

The library looks like this:

"use strict";
(function () {
    if (window && !window.StringLib) {
        const suffixes = new Map([
            ['1', 'st'],
            ['2', 'nd'],
            ['3', 'rd'],
        ]);
        window.StringLib = {
            version: '1.0.0',
            ordinalize: function (ordinal) {
                const o = '' + ordinal;
                const numFormat = parseInt(o, 10);
                if (!o || !numFormat || numFormat < 0)
                    return '';
                const last = o.at(-1) ?? '';
                return o + (suffixes.get(last) ?? 'th');
            },
        };
    }
}());

The index.d.ts for this library, which I believe is correct according to the TS documentation, is as follows:

/**
 * @name StringLib
 * @description Simple global string utilities
 */
declare namespace StringLib {
	/**
	 * version
	 * @description The version of the library
	 */
	const version: '1.0.0';

	/**
	 * ordinalize
	 * @param {string|number} ordinal The number to ordinalize
	 * @returns {string}
	 */
	function ordinalize(ordinal: string|number): string;
}

My -tests.ts file is as follows:

StringLib.ordinalize('1'); // $ExpectType string
StringLib.ordinalize(1); // $ExpectType string

I can see the types and JSDoc descriptions from the index.d.ts when I hover on the library or method in the tests file. However, when I use pnpm test the tests fail and node16 (cjs and esm) both show "Masquerading as CJS".

The exact same library, index.d.ts file, and tests used to work in dtslint version 0.0.199.

I've tried running the tests from the root of the DT repo, and also from in the types folder.

Is this a bug with dtslint? Or am I doing something wrong?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the shown index.d.ts and -tests.ts files by running pnpm test, focusing on the node16 CJS and ESM cases that report "Masquerading as CJS." Compare the result with dtslint 0.0.199 and inspect the relevant dtslint validation path. Done means the global StringLib declarations are accepted and both ordinalize assertions pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.