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

后端 未结 9 2020
不思量自难忘°
不思量自难忘° 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:26

    I had the same issues for my Angular2 project. I needed to update the Typescript (TS) library in my Angular2 project.

    1) Inside your package.json, add this to the "devDependencies" section:

    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
    

    So mine looks like:

      "devDependencies": {
        "@angular/compiler-cli": "^2.3.1",
        "@types/jasmine": "2.5.38",
        "@types/node": "^6.0.42",
        "angular-cli": "1.0.0-beta.28.3",
        "codelyzer": "~2.0.0-beta.1",
        "jasmine-core": "2.5.2",
        "jasmine-spec-reporter": "2.5.0",
        "karma": "1.2.0",
        "karma-chrome-launcher": "^2.0.0",
        "karma-cli": "^1.0.1",
        "karma-jasmine": "^1.0.2",
        "karma-remap-istanbul": "^0.2.1",
        "protractor": "~4.0.13",
        "ts-node": "~3.2.0",
        "tslint": "~5.7.0",
        "typescript": "~2.4.2"
      }
    

    2) Delete "node_modules" package and "package-lock.json" file from your project.

    3) Do "npm install" on your command line in order to install all the new TS libraries.

    0 讨论(0)
  • 2020-12-11 00:33

    This is because typescript version.

    Open command prompt or terminal. then run these commands.

    Check TypeScript version

    tsc -v
    

    should be higher than 2.4

    if not.

    install latest version of typescript globally

    npm install typescript -g
    

    Open your package.json file of the project and change typescript version like this with newly installed version

    "typescript": "~2.6.1"
    

    Then delete node_modules folder

    Clean cache using

    npm cache clean
    

    Finally run

    npm install
    

    *Note that: You can update npm using npm update but it is not sure that the typescript version will be updated *

    0 讨论(0)
  • 2020-12-11 00:33

    Inspired by Duncan's answer, I found the root cause. Although the application was using TypeScript 2.4, VS's IntelliSense was still stuck in 2.3.

    The way to resolve the issue was to download and install TypeScript 2.4 SDK and then select from the options the newer version:

    0 讨论(0)
提交回复
热议问题