Skip to content

angular-cli compiles project using TypeScript 2.5.x, irrespective of TS version in package.json #8174

@codethief

Description

@codethief

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

Globally installed:
@angular/cli: 1.4.9
node: 6.11.2
yarn: 1.2.1
os: linux x64 (Ubuntu 17.04)

node_modules of my project:
@angular/animations: 4.4.6
@angular/common: 4.4.6
@angular/compiler: 4.4.6
@angular/core: 4.4.6
@angular/forms: 4.4.6
@angular/http: 4.4.6
@angular/platform-browser: 4.4.6
@angular/platform-browser-dynamic: 4.4.6
@angular/router: 4.4.6
@angular/cli: 1.4.9
@angular/compiler-cli: 4.4.6
@angular/language-service: 4.4.6
typescript: 2.3.4

Repro steps.

  1. Create a new Angular project using $ ng new my-project.
  2. Add any code that does not compile with TypeScript 2.5 but compiles with TypeScript 2.3 to src/app/app.component.ts (see What's new in TypeScript for inspiration), e.g.:
interface Options {
    data?: string;
    timeout?: number;
    maxRetries?: number;
}

function sendMessage(options: Options) {
    // ...
}

const opts = {
    payload: "hello world!",
    retryOnFail: true,
}

// Error!
sendMessage(opts);
// No overlap between the type of 'opts' and 'Options' itself.
// Maybe we meant to use 'data'/'maxRetries' instead of 'payload'/'retryOnFail'.
  1. Run $ ng serve or $ ng build --aot

The log given by the failure.

In the above setup, running $ ng serve or $ ng build --aot fails with the following error message:

ERROR in /home/simon/tech/tests/angular-deptest2/src/app/app.component.ts (19,13): Type '{ payload: string; retryOnFail: boolean; }' has no properties in common with type 'Options'.

This means that the code is getting compiled with at least TypeScript >2.4, instead of the expected version 2.3.4 (as per my project's package.json – which angular-cli generated).

Desired functionality.

The code should compile (JIT and AOT) as my project's package.json lists "typescript": "~2.3.3" as a dependency.

Mention any other details that might be useful.

yarn link lists the following dependencies for angular-cli:

yarn list v1.2.1
…
├─ @angular/cli@1.4.9
…
│  ├─ typescript@>=2.0.0 <2.6.0
│  ├─ typescript@2.5.3
…
├─ typescript@2.3.4

and, indeed:

$ cat node_modules/@angular/cli/node_modules/typescript/package.json | grep version
    "version": "2.5.3",

confirming that TypeScript 2.5.3 was installed (which is probably because of angular-cli's chalk dependency which depends precisely on TypeScript 2.5.3 or higher). Now, I suspect that @ngtools/webpack – which angular-cli uses for compiling – will simply take its sibling TypeScript node module for compilation. So since it's also being installed to node_modules/@angular/cli/node_modules/, it will use the TypeScript version in node_modules/@angular/cli/node_modules/typescript (i.e. 2.5.3), not node_modules/typescript (which is v2.3.4).

Temporary workaround.

Add @ngtools/webpack as a dev dependency to your Angular project's package.json. Afterwards remove the node_modules folder and the yarn.lock file and resolve all dependencies from scratch using $ yarn install. @ngtools/webpack will then be installed to node_modules/@ngtools/webpack and should thus find the TypeScript version in node_modules/typescript.

Metadata

Metadata

Assignees

Labels

P0Issue that causes an outage, breakage, or major function to be unusable, with no known workaroundsneeds: more infoReporter must clarify the issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions