Definitelytyped: خطأ TS2304: لا يمكن العثور على اسم 'Set'. 815 العلامات المسموح بها<string>؛</string>

تم إنشاؤها على ٢٥ سبتمبر ٢٠١٨  ·  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)

ماذا علي أن أفعل؟

التعليق الأكثر فائدة

نفس الشيء هنا مع target و lib كـ es6 .
لقد تمكنت من إصلاح هذا عن طريق تثبيت @types/[email protected] .

ال 20 كومينتر

أضف "lib": ["es6"] أو أعلى.

أواجه نفس المشكلة ، لكن إضافة خيار lib في الملف tsconfig لم يحل المشكلة.

نسخة مطبوعة: 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 بشكل صحيح ، يرجى التأكد من تحميل lib بشكل صحيح مع الملف باستخدام include الخيار files في tsconfig.json.

@ andy-ms هل يجب أن أعيد التغيير Set ؟
بينما يجب أن يستخدم الأشخاص حقًا >es6 lib للاستخدام مع العقدة ، يمكن أن يُنظر إليه على أنه تغيير فاصل.

أعتقد أنه يمكنك تحديد 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 .

لدي نفس المشكلة وقمت بحلها باستخدام compilerOptions.types على tsconfig.json . لذلك ، قمت فقط بإدراج الحزمة المطلوبة @types في تلك الخاصية.

@ rannie-peralta تم إصلاح هذه المشكلة منذ فترة طويلة في النوع عن طريق القيام بإقرار فارغ ReadonlySet .

أنا أيضا واجهت نفس النوع من المشكلة.
package.json
{
"الاسم": "الخريطة" ،
"الإصدار": "1.0.0"،
"وصف": ""،
"main": "index.js"،
"نصوص": {
"اختبار": "صدى \" خطأ: لم يتم تحديد اختبار \ "&& خروج 1"
} ،
"الكلمات الدالة": []،
"مؤلف": ""،
"ترخيص": "ISC"،
"التبعيات": {
"typecript": "^ 3.4.5"
}
}
tsconfig.json
{
"compilerOptions": {
"الوحدة النمطية": "amd"،
"noImplicitAny": صحيح ،
"esModuleInterop": صحيح ،
"sourceMap": صحيح ،
"jsx": "رد فعل"،
"jsxFactory": "tsx"،
"الهدف": "es6"،
"زينة تجريبية": صحيح ،
"saveConstEnums": صحيح ،
"suppressImplicitAnyIndexErrors": صحيح ،
"lib": ["es2015"، "es2016"]
} ،
"تتضمن": [
"./src/*"
] ،
"استبعاد": [
"node_modules"
]
}
index.js
عرض وظيفة البيانات () {
var myMap = خريطة جديدة () ؛
myMap.set ("الاسم"، "xxxxxxxx") ؛
}
الأمر للتحويل إلى ملف js

tsc. src \ index.ts
خطأ :
خيار المترجم src/index.ts:4:21 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the lib` لـ es2015 أو ما بعده.

4 var myMap = خريطة جديدة () ؛
~~~

تم العثور على خطأ واحد

@ Praveer1981 الحل حرفيا في رسالة الخطأ.

لقد واجهت نفس المشكلة وكنت مرتبكًا لأن ملف tsconfig.json الخاص بي لديه بالضبط ما تقول رسالة الخطأ القيام به. لن يؤدي تعديل tsconfig.json أيضًا إلى إصلاح المشكلة.

عندما تمرر مسار ملف إلى tsc ، أعتقد أن tsconfig.json يتم تجاهله. هذا ما كان يحدث لي.

ينتج عن tsc src/foo.ts # أخطاء في المترجم تنص على ترقية lib / الهدف
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 };

نفس الشيء هنا مع target و lib كـ es6 .
لقد تمكنت من إصلاح هذا عن طريق تثبيت @types/[email protected] .

تضمين التغريدة
نعم ، يمكنني التأكيد. إجابتك تصلح المشكلة.
هنا https://github.com/DefinitelyTyped/DefinitelyTyped/issues/29172#issuecomment -527708175

brunobertoldi ، أود أن أضيف إلى هذا أنه كان علي فتح موجه بامتيازات المسؤول لتثبيت هذه الأنواع ، ولكن بمجرد أن فعلت ذلك ، نجحت!

بالمناسبة شكرا لك.

شكرًا على التفاصيل الإضافية ، @ thomasio101!

نفس الشيء هنا مع target و lib كـ es6 .
لقد تمكنت من إصلاح هذا عن طريق تثبيت @types/[email protected] .

ممتاز رائع!

أود أن أقترح اتباع الطريقة التي يعمل بها create-react-app

داخل خيارات المترجم

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

المحتوى في lib متروك لك تمامًا.

نفس الشيء هنا مع target و lib كـ es6 .
لقد تمكنت من إصلاح هذا عن طريق تثبيت @types/[email protected] .

يحل المشكلة

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات