Visual Studio Code Intellisense Typescript不起作用

ian*_*n.c 5 javascript typescript visual-studio-code

我已经尝试了很多年,但无论我做什么,我似乎无法让Visual Studio Code intellisense超越单个文件的打字稿.这是在Windows和Ubuntu上.

我已经包含了一个tsconfig.json文件,但它在项目规模上仍然没有任何智能感知.

我目前的测试项目包含以下内容:

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "out": "test.js"
    },
    "files": [
        "test2.ts",
        "tester.ts"
    ]
}
Run Code Online (Sandbox Code Playgroud)

tasks.json:

{
    "version": "0.1.0",
    "command": "tsc",
    "showOutput": "always",
    "windows": {
        "command": "tsc.exe"
    },
    "args": ["-p", "."],    
    "problemMatcher": "$tsc"
}
Run Code Online (Sandbox Code Playgroud)

test2.ts:

module test
{
    export class test2
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

tester.ts:

module test
{
    export class tester
    {
        public testy: test2;
    }
}
Run Code Online (Sandbox Code Playgroud)

在类测试器中,即使我将其更改为test.test2,也不会被intellisense拾取test2.向test2添加变量也无济于事.

有谁知道为什么它根本不起作用的任何可能的原因?

Suj*_*rni 14

就我而言,我必须在打字稿的 VSCode 版本上选择工作空间版本。

单击底部蓝带中的版本号

在此处输入图片说明

并在顶部栏中出现的选项中选择工作空间版本

在此处输入图片说明

希望有帮助。

  • @azizj1 和其他人可能仍然面临同样的问题。转到扩展并搜索“@builtin typescript and javascript”并启用“TypeScript and JavaScript Language Features” (8认同)
  • 我使用的是 VSCode 1.71.2 版本,Typescript 版本没有出现在与屏幕截图相同的位置。相反,我必须单击“Typescript”按钮左侧的括号,然后我可以从那里更改版本。希望它能节省几分钟的搜索时间! (3认同)
  • 我什至没有在蓝丝带中看到打字稿版本。知道为什么吗? (2认同)

Fen*_*ton 6

这是因为你告诉编译器你正在使用外部模块:

"module": "commonjs",
Run Code Online (Sandbox Code Playgroud)

但您实际上是在尝试使用内部模块:

module test
Run Code Online (Sandbox Code Playgroud)

最好选择一种方式或另一种方式.

外部模块

如果您使用的是外部模块 - 请使用:

test2.ts

export class test2 {

}
Run Code Online (Sandbox Code Playgroud)

tester.ts

import ModuleAlias = require('test2');

export class tester {
    public testy: ModuleAlias.test2;
}
Run Code Online (Sandbox Code Playgroud)

内部模块

如果您不使用外部模块,则可以使用原始代码,但删除"module": "commonjs"标志.

{
    "compilerOptions": {
        "out": "test.js"
    },
    "files": [
        "test2.ts",
        "tester.ts"
    ]
}
Run Code Online (Sandbox Code Playgroud)

  • 当我使用内部模块时,删除模块参数没有帮助,智能感知仍然不起作用.但是我确实注意到关闭了IDE并重新打开然后intellisense启动.但是如果我创建一个新文件,那么intellisense将停止为新文件工作.完全删除模块也无济于事.由于我正在使用的构建系统,因此不能使用外部模块. (2认同)