gulp-typescript编译器在readonly修饰符上抛出错误

时间:2016-09-03 04:34:39

标签: angular typescript gulp-typescript angular2-material

我刚刚将我的应用升级为使用Angular 2 rc.6Angular Material 2 alpha 8-1。这些包依赖于typescript 2,而后者则使用新的readonly修饰符。

我使用gulp-typescript来编译我的.ts文件,现在我从使用readonly修饰符的文件中收到很多错误。例如,这一行:

readonly change: Observable<MdButtonToggleChange>;

在编译期间抛出这些错误:

  

错误TS1005:&#39; =&#39;预期

     

错误TS1005:&#39;;&#39;预期

     

错误TS1005:&#39;(&#39;预期。

我认为这可能是因为gulp-typescript内部使用了typescript 1.8.10,而readonly没有readonly修饰符。

我自己的代码都没有使用.d.ts;抛出错误的唯一文件是来自Angular 2 Material包的第三方打字稿定义文件(nodes_module/)。有问题的文件都在我的tsconfig.json文件夹中,我已经尝试通过"exclude": [ "node_modules", "typings" ] 中的以下内容来忽略它们:

.d.ts

但错误仍然存​​在。

  • 我能解决这个问题吗?
  • 如果没有,是否有一种简单的方法可以让编译器忽略 if (isDir == false){ IPicture art2 = new TagLib.Picture(new TagLib.ByteVector((byte[])new System.Drawing.ImageConverter().ConvertTo(pictureBox1.Image, typeof(byte[])))); //I make the new picture here. TagLib.File file2 = TagLib.File.Create(Properties.Settings.Default.NowPlayingPath); file2.Tag.Title = SongBox.Text; file2.Tag.AlbumArtists = artist; file2.Tag.Genres = genre; file2.Tag.Year = Convert.ToUInt32(YearBox.Text); file2.Tag.Composers = composers; file2.Tag.Pictures = new IPicture[1] { art2 };//I set the picture here. file2.Save(); MessageBox.Show("You'll need to reload your song to continue listening to it.", "Settings saved."); this.Hide(); } } 个文件?

2 个答案:

答案 0 :(得分:3)

@cartant昨天得到了正确答案,但我只是在一分钟前看到它。基本上,解决方案是让gulp-typescript使用typescript 2而不是内置typescript 1.8。方法如下:

1。我在 package.json

中添加了typescript 2.0.2作为devDependency
"devDependencies": {    
  "gulp-typescript": "^2.13.6",
  "typescript": "^2.0.2",
}

2。我跑了npm install typescript@2.0.2

3。我在 gulpfile.js

中编辑了我创建gulp-typescript项目的方式

自:

var ts = require('gulp-typescript');
var tsProject = ts.createProject('tsconfig.json');

要:

var ts = require('gulp-typescript');
var tsProject = ts.createProject('tsconfig.json', {
    //Use TS version installed by NPM instead of gulp-typescript's built-in
    typescript: require('typescript')
});

More info.现在编译时错误消失了: - )

答案 1 :(得分:1)

一种解决方案是将TypeScript 2.0.2 RC依赖项添加到项目(npm install typescript@rc --save-dev)并使用非官方typescript选项将其传递给gulp-typescript

[...].pipe(ts({
    typescript: require('typescript')
}));