Skip to content

VS Code 将 .uts 文件关联为 TypeScript 后仍无法正确识别类型定义(.d.ts #139

@tmkmk

Description

@tmkmk

📌 Issue 标题:

VS Code 将 .uts 文件关联为 TypeScript 后仍无法正确识别类型定义(.d.ts


🔧 问题描述:

我正在尝试在项目中使用一种自定义的文件扩展名 .uts(例如 example.uts),并通过 VS Code 的配置将其识别为 TypeScript 文件以获得相应的语法高亮和编辑支持。

为此,我在 .vscode/settings.json 中添加了如下配置:

{
  "files.associations": {
    "*.uts": "typescript"
  }
}

目前,项目中包含以下几类文件:

  • 实现文件:.ts.uts
  • 类型声明文件:.d.ts

🧪 遇到的问题:

✅ 正常现象:

  1. .ts.uts 文件中使用类似 import A from "xxx.xxx.A" 的语句时,提示找不到模块 "xxx.xxx.A" 是预期行为。

  2. 为了解决上述错误,我在对应的 xxx.d.ts 文件中添加了如下声明:

    declare module "xxx.xxx.A" {}
  3. 当我将 .uts 文件重命名为 .ts 后,该错误 不再出现,说明类型声明是有效的。

❌ 异常现象:

  1. 当文件保留为 .uts 扩展名时,即使配置了 "*.uts": "typescript",VS Code 仍然提示:

    找不到模块“xxx.xxx.A”或其相应的类型声明。

  2. 这表明 VS Code 虽然对 .uts 文件提供了 TypeScript 的语法高亮和部分编辑功能,但在类型解析方面并未完全将其视为 .ts 文件处理。


💡 我的临时解决方案:

为了规避这个问题,我尝试了以下方式:

  1. xxx.d.ts 声明文件移出 tsconfig.jsoninclude 范围;
  2. 然后在编辑 .uts 文件的同时,手动打开并保持 xxx.d.ts 文件处于打开状态;
  3. 这样可以让 .uts 文件成功识别到该模块声明,从而消除报错。

🚨 我的目标:

我希望能够在不依赖手动打开 .d.ts 文件的前提下,让 VS Code 完整地将 .uts 文件当作标准的 .ts 文件进行处理,包括:

  • 正确识别全局模块声明(如 declare module "xxx.xxx.A"
  • 自动加载项目中的 .d.ts 文件用于类型推导

🧩 我的问题:

  1. 是否存在某种配置方式(例如修改 tsconfig.json、VS Code 插件或其他设置)可以实现这一目标?
  2. 如果当前 VS Code 对 files.associations 的支持有限,是否有其他替代方案可以实现 .uts 文件与 .ts 文件相同的行为?

📦 附加信息:

  • Visual Studio Code 版本:1.101.0 (user setup)
  • 提交哈希:dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1
  • 发布日期:2025-06-11T15:00:50.123Z
  • Electron:35.5.1
  • Chromium:134.0.6998.205
  • Node.js:22.15.1
  • V8:13.4.114.21-electron.0
  • 操作系统:Windows_NT x64 10.0.26100
  • TypeScript 版本^5.8.3

EN issue

Hi everyone,

I'm reaching out for help regarding an issue I'm facing when using custom .uts files in a TypeScript project with Visual Studio Code.

To summarize:

  • I have configured VS Code to treat .uts files as TypeScript by setting:
    {
      "files.associations": {
        "*.uts": "typescript"
      }
    }
  • My project includes both .ts, .uts, and corresponding .d.ts declaration files.
  • When importing modules like import A from "xxx.xxx.A", the error "Cannot find module 'xxx.xxx.A' or its type declarations" appears in .uts files, even though the declaration exists in xxx.d.ts.
  • Renaming the file to .ts resolves the issue, suggesting that VS Code doesn't fully treat .uts files like standard .ts files — especially in terms of type resolution and declaration loading.
  • As a workaround, I need to manually keep the .d.ts file open while editing .uts files, which is not ideal.

I would really appreciate any guidance on how to make VS Code (or TypeScript language service) treat .uts files exactly like .ts files without such workarounds.

My environment:

  • VS Code Version: 1.101.0
  • TypeScript Version: ^5.8.3
  • OS: Windows 10 (build 26100)

Questions I'd love your input on:

  1. Is there a configuration in tsconfig.json or elsewhere that can enable full TypeScript support for custom extensions like .uts?
  2. Are there known limitations or alternative approaches to achieve this?

Thanks in advance for any suggestions or insights!

Best regards,
[Your Name / Handle]
[Optional: Your Project Name or Link]

Metadata

Metadata

Assignees

No one assigned

    Labels

    *questionIssue represents a question, should be posted to StackOverflow (VS Code)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions