BeetleJuice BeetleJuice - 3 months ago 85
TypeScript Question

gulp-typescript compiler throws errors on readonly modifier

I've just upgraded my app to use

Angular 2 rc.6
and
Angular Material 2 alpha 8-1
. These packages rely on
typescript 2
and the latter makes use of the new
readonly
modifier.

I use
gulp-typescript
to compile my .ts files and I'm now getting a lot of errors from files that use the
readonly
modifier. For instance, this line:

readonly change: Observable<MdButtonToggleChange>;


Throws these errors during compilation:


error TS1005: '=' expected.

error TS1005: ';' expected.

error TS1005: '(' expected.


I think this is probably because
gulp-typescript
internally uses
typescript 1.8.10
, which does not have the
readonly
modifier.

None of my own code uses
readonly
; The only files throwing errors are third-party typescript definition files (
.d.ts
) from
Angular 2 Material
packages. The files in question are all within my
nodes_module/
folder, and I've tried to ignore them by having the following in
tsconfig.json
:

"exclude": [
"node_modules",
"typings"
]


The errors still show up though.


  • Can I solve this?

  • If not, is there an easy way to get the compiler to ignore
    .d.ts
    files?


Answer

One solution would be to add a TypeScript 2.0.2 RC dependency to your project (npm install typescript@rc --save-dev) and to pass it to gulp-typescript using the unofficial typescript option:

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