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

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

    In Visual Studio 2017 version 15.3 and later, the TypeScript version used is bound to individual projects.

    Reference - https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017

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

    If you are using Visual studio 2017 Community version. you will not find TypeScript intellisense in Tools/Options. You should edit the project file .jsproj. TypeScriptToolsVersion

    and update TypeScriptToolsVersion to 2.6.2 or latest version.

    0 讨论(0)
  • 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.

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

    typecast with 'any' will work out:

    enum StepType {
        ENGLISH = <any>'S',
    }
    
    0 讨论(0)
  • 2020-12-11 00:20

    This is the error you get when compiling with a version of typescript older than 2.4. All I can suggest is that your copy of Visual Studio is somehow picking up its own older version of typescript rather than using the newer one installed in your project. See the wiki https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017 for instructions on updating typescript.

    PS C:\temp> cat t.ts
    enum StepType {
        Start = 'S',
        Activity = 'A',
        Decision = 'D',
        End = 'E'
    }
    PS C:\temp> node somepath\node_modules\typescript\bin\tsc --version
    Version 2.2.2
    PS C:\temp> node somepath\node_modules\typescript\bin\tsc t.ts
    t.ts(2,13): error TS2322: Type '"S"' is not assignable to type 'StepType'.
    t.ts(3,16): error TS2322: Type '"A"' is not assignable to type 'StepType'.
    t.ts(4,16): error TS2322: Type '"D"' is not assignable to type 'StepType'.
    t.ts(5,11): error TS2322: Type '"E"' is not assignable to type 'StepType'.
    PS C:\temp> tsc --version
    Version 2.4.1
    PS C:\temp> tsc t.ts
    PS C:\temp>
    
    0 讨论(0)
  • 2020-12-11 00:22

    If you are like me, using VS but not in project (just opening a folder), then I just had to install the latest version of TS for VS:

    https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.typescript-27-vs2017

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