[英]String Enum in typescript error
我試圖向我的Angular 2項目中添加一個字符串枚舉,但是在使用npm啟動項目期間發生錯誤:
ERROR in e:/projects/dbtool-fullstack/dbtool-client/src/app/shared/models/full-m
odels/enums/Sex.ts (2,10): Type '"Male"' is not assignable to type 'Sex'.
ERROR in e:/projects/dbtool-fullstack/dbtool-client/src/app/shared/models/full-m
odels/enums/Sex.ts (3,12): Type '"Female"' is not assignable to type 'Sex'.
ERROR in e:/projects/dbtool-fullstack/dbtool-client/src/app/shared/models/full-m
odels/enums/Sex.ts (4,8): Type '"NA"' is not assignable to type 'Sex'.
枚舉聲明:
export enum Sex {
Male = 'Male',
Female = 'Female',
NA = 'NA'
}
和打字稿版本:
e:\projects\dbtool-fullstack\dbtool-client>tsc --version
Version 2.5.3
e:\projects\dbtool-fullstack\dbtool-client>npm list -g --depth=0
C:\Users\pavlo\AppData\Roaming\npm
+-- @angular/cli@1.4.4
+-- npm@5.5.1
`-- typescript@2.5.3
此類型的Enum聲明應從2.4.0版本開始工作
是什么原因造成的?
更新
當我使用ng -v時:
e:\projects\TESTENUMS>ng -v
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.4.9
node: 6.11.3
os: win32 ia32
@angular/animations: 4.4.6
@angular/common: 4.4.6
@angular/compiler: 4.4.6
@angular/core: 4.4.6
@angular/forms: 4.4.6
@angular/http: 4.4.6
@angular/platform-browser: 4.4.6
@angular/platform-browser-dynamic: 4.4.6
@angular/router: 4.4.6
@angular/cli: 1.4.9
@angular/compiler-cli: 4.4.6
@angular/language-service: 4.4.6
typescript: 2.3.4
在這種情況下,TS的版本為2.3.4。 我不知道它從哪里來。
重要
事實證明,當前版本的Angular(4.4.6)不支持Typescript> = 2.4.0。 從v5.0.0開始應支持該功能,目前尚不可用。
更改項目的打字稿版本的操作:
nmp install
,之后應該可以運行了。 ng -v
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.