microsoft / microsoft/TypeScript

Incorrect error message on export assignment in `.d.ts` file

未关闭
#33,341 0 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

In Discussion Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

TypeScript Version: 3.5.3

Search Terms:

An export assignment cannot be used in a namespace.

Code

This is pretty niche, but I think it has general implications...

The current /lib/typescript.d.ts file is modelled as essentially a CommonJS file where at the end of the file, it has export = ts; which exports the whole namespace. The actual /lib/typescript.js though will place itself in the global scope as ts if it isn't loaded as a CommonJS module. In order to express that ts is available as a global variable, it requires code like this to be placed in a .d.ts file:

import * as _ts from "typescript";

declare global {
  namespace ts {
    export = _ts;
  }
}

Other methods of expressing this, like declare var ts: typeof _ts; only make the function shape of ts available, while the interfaces and type aliases and other type information is not accessible.

Expected behavior:

No errors.

Actual behavior:

The error TS1063: An export assignment cannot be used in a namespace occurs, but in VSCode, if the file is named globals.d.ts this error does not occur. 🤷‍♂ In all cases the code above does work like expected, making both the functional and type exports of the namespace available for code to be checked against.

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先使用提供的声明片段复现该诊断,然后将其与文件命名为 globals.d.ts 时的行为进行比较。阅读 /lib/typescript.d.ts 中相关的声明,以及 /lib/typescript.js 中的运行时上下文。当有效模式不报告错误,同时保留功能导出和类型导出时,即表示完成。

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

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

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