typescript Visual Studio Code 智能感知打字稿不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30394831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:01:00  来源:igfitidea点击:

Visual Studio Code Intellisense Typescript not working

javascripttypescriptvisual-studio-code

提问by ian.c

I've been trying for ages but I cannot seem to get Visual Studio Code intellisense working beyond a single file for typescript no matter what I do. This is on windows as well as Ubuntu.

我已经尝试了很长时间,但无论我做什么,我似乎都无法让 Visual Studio Code 智能感知在打字稿的单个文件之外工作。这是在 Windows 和 Ubuntu 上。

I've included a tsconfig.json file but it still doesn't have any intellisense on a project scale.

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

My current test project contains the following:

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

tsconfig.json:

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "out": "test.js"
    },
    "files": [
        "test2.ts",
        "tester.ts"
    ]
}

tasks.json:

任务.json:

{
    "version": "0.1.0",
    "command": "tsc",
    "showOutput": "always",
    "windows": {
        "command": "tsc.exe"
    },
    "args": ["-p", "."],    
    "problemMatcher": "$tsc"
}

test2.ts:

测试2.ts:

module test
{
    export class test2
    {

    }
}

tester.ts:

测试员.ts:

module test
{
    export class tester
    {
        public testy: test2;
    }
}

In the class tester test2 isn't picked up by intellisense, even if i change it to test.test2. Adding variables to test2 doesn't help either.

在类 test2 中,intellisense 不会接收到 test2,即使我将其更改为 test.test2。向 test2 添加变量也无济于事。

Does anyone know any possible causes as to why it isn't working at all?

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

回答by Fenton

It's because you have told the compiler you are using external modules:

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

"module": "commonjs",

But you are actually trying to use internal modules:

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

module test

It is best to choose one way or the other.

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

External Modules

外部模块

If you are using external modules - use:

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

test2.ts

测试2.ts

export class test2 {

}

tester.ts

测试人员.ts

import ModuleAlias = require('test2');

export class tester {
    public testy: ModuleAlias.test2;
}

Internal Modules

内部模块

If you aren't using external modules, you can use your original code, but remove the "module": "commonjs"flag.

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

{
    "compilerOptions": {
        "out": "test.js"
    },
    "files": [
        "test2.ts",
        "tester.ts"
    ]
}

回答by Sujit Y. Kulkarni

In my case, I had to select the work space version over VSCode version of typescript.

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

Click on the version number in the blue ribbon at the bottom

单击底部蓝带中的版本号

enter image description here

在此处输入图片说明

And select work space version in the options appearing in the top bar

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

enter image description here

在此处输入图片说明

Hope that helps.

希望有帮助。

回答by Guilherme Defreitas

Intellisense for node_modules folder not work in versions <= 2.0.3 (current).

node_modules 文件夹的 Intellisense 在版本 <= 2.0.3(当前)中不起作用。

It will be available in version 2.0.5 (https://github.com/Microsoft/TypeScript/issues/9323)

它将在 2.0.5 版(https://github.com/Microsoft/TypeScript/issues/9323)中可用

To temporarily fix you can use the night build: npm install -g typescript@next

要临时修复,您可以使用夜间构建: npm install -g typescript@next

It worked for me.

它对我有用。

回答by Gaurav vijayvargiya

This is not a problem with typescript.

这不是打字稿的问题。

In visual code 1.9 onwards they have security issues.It try to read some data from your windows drive and if you do not have rights then it do not show intellisense.

在visual code 1.9以后,他们有安全问题。它尝试从你的windows驱动器读取一些数据,如果你没有权限,那么它不会显示智能感知。

If you do not have full admin rights on your system then uninstall 1.9.x version and install VSCodeSetup-1.8.1

如果您的系统没有完全管理员权限,请卸载 1.9.x 版本并安装 VSCodeSetup-1.8.1

this works for me.

这对我有用。

回答by basarat

I would update the tsconfig.json to be relative paths:

我会将 tsconfig.json 更新为相对路径:

{
    "compilerOptions": {
        "out": "test.js"
    },
    "files": [
        "./test2.ts",
        "./tester.ts"
    ]
}

Also as metioned by steve don't mix modules (although that wouldn't case an error here). And also, perhaps don't use out : https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md

同样正如史蒂夫所提到的,不要混合模块(尽管这里不会出现错误)。而且,也许不要用完:https: //github.com/TypeStrong/atom-typescript/blob/master/docs/out.md