Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Declarations for a global library incorrectly interpreted as module

未关闭
#1,192 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清

调研方向

使用所示的 index.d.ts 和 -tests.ts 文件运行 pnpm test 以重现故障,重点关注报告 "Masquerading as CJS." 的 node16 CJS 和 ESM 情况。将结果与 dtslint 0.0.199 进行比较,并检查相关的 dtslint 验证路径。当全局 StringLib 声明被接受且两个 ordinalize 断言都通过时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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?

主要语言
TypeScript
星标
422
派生
238
平均合并
1 天 4 小时
30 天内合并 PR
8

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/DefinitelyTyped-tools 的其他 Issue

查看 microsoft/DefinitelyTyped-tools 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。