TypeScript's string enums - “Type … is not assignable to type …”

后端 未结 9 2047
不思量自难忘°
不思量自难忘° 2020-12-10 23:45

I have recently upgraded the version of TypeScript from 2.3.4 to 2.4.0 hoping to use the string enums. To my dismay, however, I have been greeted with the error messages:

9条回答
  •  抹茶落季
    2020-12-11 00:18

    For me, the problem was that @angular/cli was using a lower version of Typescript. Check out your lock file. It was showing a requirement of <2.4.0. Our project uses yarn.lock, for example.

    When it compiled, it was throwing an error related to the lower version of Typescript. To fix the problem, I added the compatible flag ^ to the front. So for us, it started as:

    "@angular/cli": "1.2.5"

    ...changed to:

    "@angular/cli": "^1.2.5"

    This seems to fix the issue. It's worth noting that it essentially forces cli to use the workspace version of Typescript. For us, this is 2.4.0, which this version of cli isn't technically compatible with (since it requires <2.4.0). It throws a warning when compiling, but it has worked successfully for us for the time being.

提交回复
热议问题