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

9

38

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:

Severity  Code    Description Project File    Line    Suppression State
Error TS2322  Type '"E"' is not assignable to type
'StepType'.   ClientApp (tsconfig
project)  Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts  17  Active
Error TS2322  Type '"S"' is not assignable to type
'StepType'.   ClientApp (tsconfig
project)  Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts  14  Active
Error TS2322  Type '"A"' is not assignable to type
'StepType'.   ClientApp (tsconfig
project)  Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts  15  Active
Error TS2322  Type '"D"' is not assignable to type
'StepType'.   ClientApp (tsconfig
project)  Z:\SMART\Smart\Smart\ClientApp\app\models\process.model.ts  16  Active

The error messages apply to the following code snippet (with the line numbers):

13. export enum StepType {
14.    Start = 'S',
15.    Activity = 'A',
16.    Decision = 'D',
17.    End = 'E'
18. }

I am using Visual Studio 2017 which claims TypeScript 2.4.0 is installed:

enter image description here

I searched through TypeScript's issues, but without luck. Does anybody know how to fix it?

Blida answered 19/7, 2017 at 13:2 Comment(6)
Source of the error is in process.model.ts file. Can you share it?Discourse
The errors are definitely associated with the code snippet. Once you remove the assignment (or use numbers instead), the errors go away. Something tells me, it is a bug in the TypeScript implementationBlida
Error states "Type E is not assignable to StepType". The snippet you provided is declaration, but error clearly says assignment. One line lower you have info about file and line (process.model.ts line 17).Discourse
@ArekŻelechowski read the message. It is complaining about the assignment of types not values. This is indeed the message you get when you use the code in the snippet on a version of typescript prior to 2.4. I suspect that Visual Studio is still managing to pick up an older version even if the project has the newer one installed.Newbold
@Newbold Thanks for nice explanation. Good to know for the future, error seems to be quite mysterious. Also, at the point of my comment there were no info about line numbers and I thought that process.model.ts does not contain StepType definition, but code which uses this enum.Discourse
I resolved this issue only after deleting my VS and installing the newest version of it (VS Community 2017, v. 15.5.2 in my case). Even just updating of VS did not help. No thoughts about this behaviorHorsepowerhour
B
5

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

Blida answered 19/7, 2017 at 15:11 Comment(2)
This didn't give me the option to change it in the settings, but the Typescript 2.4 SDK did solve my problem. Thanks.Twoway
Same for me in WebStorm...TypeScript version was fixed to 2.2 in settings. When I selected detect automatically it worked!Snailfish
R
30

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 *

Rorqual answered 6/11, 2017 at 23:12 Comment(2)
Currently running tsc 2.8.3 in an angular cli project of v1.7.3 but I still get the error.Libertylibia
Me too, I have greater typescript version and still getting the errorRyle
N
8

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>
Newbold answered 19/7, 2017 at 14:56 Comment(0)
B
5

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

Blida answered 19/7, 2017 at 15:11 Comment(2)
This didn't give me the option to change it in the settings, but the Typescript 2.4 SDK did solve my problem. Thanks.Twoway
Same for me in WebStorm...TypeScript version was fixed to 2.2 in settings. When I selected detect automatically it worked!Snailfish
G
1

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.

Grosvenor answered 17/10, 2017 at 22:56 Comment(0)
C
1

typecast with 'any' will work out:

enum StepType {
    ENGLISH = <any>'S',
}
Cloninger answered 9/12, 2020 at 5:59 Comment(0)
W
0

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.

Washko answered 6/2, 2018 at 1:5 Comment(0)
K
0

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

Kyl answered 29/6, 2018 at 12:18 Comment(0)
I
0

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

Individual answered 29/4, 2019 at 7:13 Comment(0)
E
0

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.

Euonymus answered 14/8, 2019 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.