gpt4 book ai didi

node.js - typescript 错误中的字符串枚举

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:18 26 4
gpt4 key购买 nike

我尝试将字符串枚举添加到我的 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'
}

和 typescript 版本:

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。但我不知道它从哪里来。

最佳答案

重要

事实证明,当前版本的 Angular (4.4.6) 不支持 Typescript >=2.4.0。从 v5.0.0 开始应该支持,目前还没有。

更改项目 typescript 版本的操作:

  1. 有必要在 package.json 中设置正确的 typescript 版本(在我的例子中为 2.5.3)。
  2. 在项目根目录(包含 package.json 的文件夹)中运行 nmp install,之后应该可以工作。
  3. 要确保使用正确的 typescript 版本,请运行 ng -v

关于node.js - typescript 错误中的字符串枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47014910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com