Definitelytyped: 错误 TS2304:找不到名称“设置”。 815 allowedNodeEnvironmentFlags:设置<string>;</string>

创建于 2018-09-25  ·  20评论  ·  资料来源: DefinitelyTyped/DefinitelyTyped

D:\workshop\www
λ node -v
v8.12.0

D:\workshop\www
λ npm -v
6.4.1

D:\workshop\www
λ npm install typescript -g
C:\Users\dell\AppData\Roaming\npm\tsc -> C:\Users\dell\AppData\Roaming\npm\node_modules\typescript\bin\tsc
C:\Users\dell\AppData\Roaming\npm\tsserver -> C:\Users\dell\AppData\Roaming\npm\node_modules\typescript\bin\tsserver
+ [email protected]
updated 1 package in 3.061s

D:\workshop\www
λ tsc --version
Version 3.0.3

D:\workshop\www
λ mkdir typescript_learning

D:\workshop\www
λ cd typescript_learning

D:\workshop\www\typescript_learning
λ npm init -y
Wrote to D:\workshop\www\typescript_learning\package.json:

{
  "name": "typescript_learning",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}



D:\workshop\www\typescript_learning  ([email protected])
λ tsc --init
message TS6071: Successfully created a tsconfig.json file.

D:\workshop\www\typescript_learning  ([email protected])
λ npm install @types/node --dev-save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

+ @types/[email protected]
added 1 package from 30 contributors in 1.657s

D:\workshop\www\typescript_learning  ([email protected])
λ touch helloworld.ts

D:\workshop\www\typescript_learning  ([email protected])
λ tsc helloworld.ts
node_modules/@types/node/index.d.ts:815:38 - error TS2304: Cannot find name 'Set'.

815         allowedNodeEnvironmentFlags: Set<string>;

helloworld.ts

var a:string = "HelloWorld"
console.log(a)

我该怎么办?

最有用的评论

targetlibes6相同。
我设法通过安装@types/[email protected]来解决这个问题。

所有20条评论

添加"lib": ["es6"]或更高。

我遇到了同样的问题,但是在tsconfig文件上添加lib选项并没有解决它。

打字稿版本:3.1.1
节点类型版本:10.11.5

重现步骤

$ mkdir test
$ cd test
$ npm init -y
$ npm install --save-dev typescript @types/node
$ echo 'console.log("test")' > index.ts
$ ./node_modules/.bin/tsc --init

Add lib: "es2015" to tsconfig.json

$ ./node_modules/.bin/tsc index.ts

node_modules/@types/node/index.d.ts:815:38 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

815         allowedNodeEnvironmentFlags: Set<string>;

我和很多人讨论过这个问题,如果lib没有修复它,那么在某些时候你的 tsconfig 没有被正确加载,请确保使用include与文件一起正确加载 lib tsconfig.json 中的files选项。

@andy-ms 我应该恢复Set更改吗?
虽然人们真的应该使用>es6库来与 node 一起使用,但这_可以_被视为一个重大变化。

我认为您可以在 $#$2$# interface Set<T> {}中定义低于interface SetConstructor {}node/index.d.ts ,以便它可以与--lib es5一起使用。

同样的问题。

☁  jest-codelab [master] ⚡  $(npm bin)/tsc ./src/@types/index.ts
node_modules/@types/node/index.d.ts:815:38 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

815         allowedNodeEnvironmentFlags: Set<string>;
                                         ~~~

tsconfig.json

"lib": ["es2015", "es2016"]

package.json

"devDependencies": {
    "@types/jest": "^23.3.3",
    "@types/node": "^10.11.4",
    "jest": "^23.6.0",
    "ts-jest": "^23.10.4",
    "tslint": "^5.11.0",
    "typescript": "^3.1.1"
  }

@mrdulin我可以告诉你没有安装最新的@types/node (当前是 10.12.0),因为它是allowedNodeEnvironmentFlags: ReadonlySet<string>;并且interface ReadonlySet<T> {}@types/node/index.d.ts中声明

@andy-ms 谢谢。 在npm rm @types/node -D之后,这个问题就消失了。

我有同样的问题,我在tsconfig.json compilerOptions.types解决了它。 所以基本上我只在该属性中列出了所需的@types包。

@rannie-peralta 这个问题早已通过空的ReadonlySet声明在类型上得到修复。

我也遇到了同样的问题。
包.json
{
“名称”:“地图”,
“版本”:“1.0.0”,
“描述”: ””,
“主”:“index.js”,
“脚本”:{
"test": "echo \"错误:没有指定测试\" && exit 1"
},
“关键字”:[],
“作者”: ””,
“许可证”:“ISC”,
“依赖”:{
“打字稿”:“^3.4.5”
}
}
tsconfig.json
{
“编译器选项”:{
“模块”:“AMD”,
“noImplicitAny”:是的,
“esModuleInterop”:是的,
“源地图”:是的,
“jsx”:“反应”,
"jsxFactory": "tsx",
“目标”:“es6”,
“experimentalDecorators”:是的,
“preserveConstEnums”:真,
“suppressImplicitAnyIndexErrors”:真,
“lib”:[“es2015”,“es2016”]
},
“包括”: [
“./src/*”
],
“排除”: [
“节点模块”
]
}
index.js
函数显示数据(){
var myMap = new Map();
myMap.set("名称","xxxxxxxx");
}
转换成js文件的命令

tsc .src\index.ts
错误 :
es2015 或更高版本的src/index.ts:4:21 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the lib` 编译器选项。

4 var myMap = new Map();
~~~

发现 1 个错误。`

@Praveer1981解决方案实际上在错误消息中。

我遇到了同样的问题并且很困惑,因为我的 tsconfig.json 完全符合错误消息的要求。 进一步修改我的 tsconfig.json 不会解决问题。

当您将文件路径传递给tsc时,我相信tsconfig.json会被忽略。 这就是发生在我身上的事情。

tsc src/foo.ts # 导致编译器错误说明升级我的 lib/target
tsc # 工作正常,因为我的 tsconfig.json 指定了 es2015 或更高版本的目标

因此,如果您得到这个并且您的 tsconfig.json 看起来不错,请仔细检查您是否将自定义路径传递给编译器,并可能忽略您的配置。

同样在这里

node_modules/@types/react/index.d.ts:377:23 - 错误 TS2583:找不到名称“Set”。 您需要更改目标库吗? 尝试将lib编译器选项更改为 es2015 或更高版本。

interactions: Set<SchedulerInteraction>,

配置:

{
  "compilerOptions": {
    "target": "es5",
    "target: "es5", // <= doesn't help
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
      "es2015", // <= doesn't help
      "es6", // <= doesn't help
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "sourceMap": true,
    "baseUrl": "./",
    "jsx": "preserve"
  },
  "include": [
    "./**/*",
    "*.tsx"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

我解析的文件抛出:

class Greeter {
  public greeting: string;
  constructor(msg: string) {
    this.greeting = msg;
  }
}

let g = new Greeter('hello');

export { Greeter };

targetlibes6相同。
我设法通过安装@types/[email protected]来解决这个问题。

@brunobertoldi
是的,我可以确认。 你的回答解决了这个问题。
这里https://github.com/DefinitelyTyped/DefinitelyTyped/issues/29172#issuecomment -527708175

@brunobertoldi ,我想补充一点,我必须以管理员权限打开一个提示来安装这些类型,但是一旦我这样做了,它就起作用了!

谢谢,顺便说一句。

感谢您提供更多详细信息,@thomasio101!

targetlibes6相同。
我设法通过安装@types/[email protected]来解决这个问题。

好酷!

我建议遵循create-react-app的工作方式

compilerOptions里面

    ...
    "skipLibCheck": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
   ...

lib 中的内容完全取决于您。

targetlibes6相同。
我设法通过安装@types/[email protected]来解决这个问题。

解决问题

此页面是否有帮助?
0 / 5 - 0 等级