TypeScript's string enums - "Type ... is not assignable to type ..."

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 *


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>

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. VS IntelliSense was not updated

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

enter image description here