我尝试将一个字符串枚举添加到我的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'.
Enum声明:
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
这种类型的枚举声明应该从版本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。我不知道它来自哪里。
答案 0 :(得分:0)
重要强>
事实证明,当前版本的Angular(4.4.6)并不支持Typescript> = 2.4.0。它应该从v5.0.0开始支持,该版本尚不可用。
更改项目打字稿版本的操作:
nmp install
,然后运行。ng -v