Skip to content

Declarations for a global library incorrectly interpreted as module #1192

@danwellman

Description

@danwellman

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions