我正在尝试使用gulp-typescript,如果有任何错误,我无法让它发出任何信息。
tsc命令,就像它应该的那样,不管错误如何,都会成功地发出,我希望gulp-typescript也会这样做。
但相反,我得到了一个模糊的错误:
Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.
[10:45:09] Using gulpfile ~\Desktop\test\gulpfile.js
[10:45:09] Starting 'default'...
src\script.ts(2,3): error TS2339: Property 'gulp' does not exist on type '{}'.
TypeScript: 1 semantic error
TypeScript: emit succeeded (with errors)
[10:45:09] 'default' errored after 525 ms
[10:45:09] Error: TypeScript: Compilation failed
at Output.mightFinish (C:\Users\Pouria\Desktop\test\node_modules\gulp-typescript\release\output.js:131:43)
at C:\Users\Pouria\Desktop\test\node_modules\gulp-typescript\release\output.js:44:22
at processTicksAndRejections (internal/process/task_queues.js:93:5)项目结构很简单:一个tsconfig.json文件、一个package.json文件、一个gulpfile.js文件和一个包含单个script.ts文件的src文件夹。
我的tsconfig.json文件:
{
"compilerOptions": {
"outDir": "./dist",
"allowJs": true,
"target": "es2017",
"lib": [ "es2017" ],
"noEmitOnError": false,
"noEmit": fals,
},
"include": ["./src/**/*"],
}我的package.json文件:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"dependencies": {
"gulp": "^4.0.2",
"gulp-typescript": "^6.0.0-alpha.1",
"typescript": "^4.7.4"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}我的gulpfile.js
var gulp = require('gulp');
var ts = require('gulp-typescript');
var tsProject = ts.createProject("tsconfig.json");
gulp.task('default', function(){
return tsProject.src().pipe(tsProject()).js.pipe(gulp.dest("dist"));
});我的script.ts文件:
var v = {};
v.gulp = "does not work";运行npm ls --depth 0的结果
test@1.0.0 C:\Users\Pouria\Desktop\test
+-- gulp@4.0.2
+-- gulp-typescript@6.0.0-alpha.1
`-- typescript@4.7.4发布于 2022-08-12 08:43:21
因此,在深入研究github问题部分之后,发现如果流程中有任何错误,则gulp实际上是故意失败的。
为了解决这个问题,您的gulp文件应该如下所示:
var gulp = require('gulp');
var ts = require('gulp-typescript');
var tsProject = ts.createProject("tsconfig.json");
gulp.task('default', function(){
return tsProject.src().pipe(tsProject())
.on("error", () => { /* Ignore compiler errors */})
.js.pipe(gulp.dest("dist"));
});即添加要处理/忽略错误的.on("error", () => { /* Ignore compiler errors */})。
我花了很多小时试图弄清楚到底是怎么回事。遗憾的是,这种行为在任何地方都没有提到,在教程中,在入门页面中,在包描述中等等。
这来自于typescript 官方手册,解释了为什么类型记录即使有错误也会编译:
考虑到tsc报告了我们代码的错误,这可能有点令人惊讶,但是--这是基于类型记录的核心值之一:很多时候,你会比TypeScript更清楚。 ..。 所以TypeScript不会挡你的路。当然,随着时间的推移,您可能希望对错误更加防御性,并使TypeScript的行为更加严格。在这种情况下,您可以使用noEmitOnError编译器选项。尝试更改您的hello.ts文件并使用该标志运行tsc。
因此,如果在某个地方提到gulp-typescript和tsc本身之间存在着这种鲜明的对比,那将是非常有帮助的。
https://stackoverflow.com/questions/73329972
复制相似问题