1

我独自跟着VS Code typescript配置。如何在VScode中设置打字稿?

https://code.visualstudio.com/Docs/languages/typescript

我安装我的tsconfig.json像

{ 
    "compilerOptions": { 
     "module": "system", 
     "noImplicitAny": true, 
     "removeComments": true, 
     "preserveConstEnums": true, 
     "outFile": "built/local/tsc.js", 
     "sourceMap": true 
    }, 
    "include": [ 
     "**/*" 
    ], 
    "exclude": [ 
     "node_modules", 
     "**/*.spec.ts" 
    ] 
} 

,我的任务运行

{ 
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format 
    "version": "0.1.0", 
    "command": "tsc", 
    "isShellCommand": true, 
    "args": ["-p", "."], 
    "showOutput": "always", 
    "problemMatcher": "$tsc" 
} 

我的TS码

class StartUp { 
    public static main(): number { 
     console.log('Helle Workd'); 
     return 5; 
    } 
} 
console.log('test'); 

StartUp.main(); 

对于一些reaso n,当我按下cmd + shift + B(build)时,在输出窗口中看不到任何输出。我确实看到如下错误:

hello.ts(8,1): error TS2304: Cannot find name 'ddd'. 

如果我在代码中随机添加ddd字符串。

有人可以帮我解决这个问题吗?非常感谢!

+0

是生成的文件好吗?也许你可以运行“tsc -p”。在控制台。如果没有错误,命令将不会输出。 – Yuan

回答

1

命令“tsc -p。”在编译时不会输出任何错误,并且会生成所有已编译的JavaScript/SourceMap文件,因此您无法在VSCode的输出窗口中看到任何文件。只需在控制台中键入并运行该命令即可。

您可以添加选项“--diagnostics”以使命令输出一些信息。

{ 
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format 
    "version": "0.1.0", 
    "command": "tsc", 
    "isShellCommand": true, 
    "args": ["-p", ".", "--diagnostics"], 
    "problemMatcher": "$tsc" 
} 

输出:

Files:   2 
Lines:  18225 
Nodes:  73338 
Identifiers: 24828 
Symbols:  18969 
Types:   4609 
Memory used: 62579K 
I/O read:  0.00s 
I/O write: 0.01s 
Parse time: 0.24s 
Bind time: 0.12s 
Check time: 0.54s 
Emit time: 0.06s 
Total time: 0.96s 

另请参阅http://www.typescriptlang.org/docs/handbook/compiler-options.html

0

安装的应用程序结构 设置基本的HTML NPM初始化--yes NPM安装精简版服务器的所有选项--save- dev npm install --save-dev typescript @ types/node @ types/jasmine @ types/core-js 将脚本精简版添加到package.json的脚本中 创建一个app文件夹 添加app.js 创建tsconfig.json 添加下列选项

{ 
"compilerOptions": { 
"target": "es5", 
"module": "commonjs", 
"moduleResolution": "node", 
"sourceMap": true, 
"emitDecoratorMetadata": true, 
"experimentalDecorators": true, 
"removeComments": false, 
"noImplicitAny": false, 
"lib": ["es2015", "dom"] 
} 
} 

添加以下到的package.json文件

{ 


"name": "Angular2", 

"version": "1.0.0", 

"description": "", 

"main": "index.js", 

"scripts": 
{ 

    "start": "concurrently \"npm run tsc:w\" \"npm run lite\"", 

    "lite": "lite-server", 
"tsc": "tsc", 
"tsc:w": "tsc -w" 
}, 

    "keywords": [], 
"author": "", 
"license": "ISC", 
"devDependencies": 
{ 

    "@types/core-js": "^0.9.43", 

    "@types/jasmine": "^2.8.2", 

    "@types/node": "^8.0.53", 

    "concurrently": "^3.5.1", 

    "lite-server": "^2.3.0", 

    "typescript": "^2.6.1" 
    }, 


    "dependencies": 
{ 

    "types": "^0.1.1" 

} 

}