[typescript] TypeScript : 중복 식별자 ‘IteratorResult’

tsc전역 적으로 설치 한-을 통해 컴파일하려고하는데 오류가 발생합니다.

~/AppData/Roaming/nvm/v11.15.0/node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6 - error TS2300: Duplicate identifier 'IteratorResult'.

41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
        ~~~~~~~~~~~~~~

  node_modules/@types/node/index.d.ts:170:11
    170 interface IteratorResult<T> { }
                  ~~~~~~~~~~~~~~
    'IteratorResult' was also declared here.

node_modules/@types/node/index.d.ts:170:11 - error TS2300: Duplicate identifier 'IteratorResult'.

170 interface IteratorResult<T> { }
              ~~~~~~~~~~~~~~

~/AppData/Roaming/nvm/v11.15.0/node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6
    41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
            ~~~~~~~~~~~~~~
    'IteratorResult' was also declared here.


Found 2 errors.

내가 가진 @types/node버전 10.1.0 설치. ( @latest자체 문제가 있습니다 …)

tsconfig.json

{
  "compilerOptions": {
    "target": "es2018",
    "moduleResolution": "node",
    "module": "commonjs",
    "jsx": "react",
    "lib": [
      "dom",
      "es2018",
      "dom.iterable",
      "scripthost"
    ],
    "typeRoots": [
      "./node_modules/@types",
      "./types"
    ],
    "types": [],

    "alwaysStrict": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,

    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,

    "sourceMap": true,

    "outDir": "dist"
  },
  "files": [
    "app/index.tsx"
  ],
  "include": [
    "app/**/*.ts",
    "app/**/*.tsx",
    "test/**/*.ts",
    "test/**/*.tsx",
    "node_modules/@types/**/*.d.ts",
    "./types/**/*.d.ts"
  ],
  "exclude": [
    "dist"
  ]
}

typescript전역 적으로 제거 하고 실행 npx tsc하면 작동하지만 typescript전역 적으로 설치하고 실행하는 데 아무런 문제가 없습니다 . 결국, 이것이 전 세계적으로 설치하는 요점입니다.

그 동안 tsc라는 별칭을 사용하는 해결 방법이 있습니다 (Windows에서 git bash를 사용하고 있습니다).

alias tsc="path/to/project/node_modules/.bin/tsc.cmd"



답변

GitHub- https: //github.com/microsoft/TypeScript/issues/32333 에서 관련 문제를 발견했습니다 . @rbuckton은 업그레이드를 제안했습니다 @types/node. 그것은 나를 위해 일했습니다.


답변

내 angular 8 앱에서 is 오류가 발생했으며 수락 된 답변을 포함하여 여기에서 제안한 모든 제안을 시도한 후에도 문제를 해결할 수 없습니다. 오류없이 컴파일 된 이전 앵귤러 6 앱을 살펴 보았는데 다음을 포함하여 라이브러리 검사를 건너 뛸 수 있다는 것을 깨달았습니다.

“skipLibCheck”: 참

tsconfig.json 파일에 추가합니다. 내 앱이 문제없이 잘 실행된다는 사실을 감안하여이 접근 방식을 사용하기로 결정했습니다. 다음은 내 tsconfig.json 파일의 전체 구성입니다.

{ "compileOnSave": false,
  "compilerOptions": {
  "baseUrl": "./",
  "outDir": "./dist/out-tsc",
  "sourceMap": true,
  "declaration": false,
  "downlevelIteration": true,
  "experimentalDecorators": true,
  "module": "esnext",
  "moduleResolution": "node",
  "importHelpers": true,
  "target": "es2015",
  "typeRoots": [
  "node_modules/@types"
  ],
  "lib": [
    "es2018",
    "dom"
  ],
  "skipLibCheck": true
  },
  "angularCompilerOptions": {
  "fullTemplateTypeCheck": true,
  "strictInjectionParameters": true
  }
}

이 구성 후 더 이상 오류가 없습니다.
참고 : 문제가 해결되었다는 의미는 아니지만 적어도 오류를 일으킨 버그를 건너 뛸 수있었습니다. 내 앱이 예상대로 실행되고 있기 때문에 지금은이 오류와 관련이 없다고 생각했습니다.


답변

귀하의 포함 섹션 때문이라고 생각합니다.

"include": [
    "app/**/*.ts",
    "app/**/*.tsx",
    "test/**/*.ts",
    "test/**/*.tsx",
    "node_modules/@types/**/*.d.ts",
    "./types/**/*.d.ts"
  ]

일반적으로 * .d.ts 파일을 명시 적으로 포함 할 필요는 없습니다. 그리고 아마도 다른 라이브러리 (또는 노드 유형)에서 파일을 선언하지 않을 것입니다.

tsconfig“제외” 섹션 "node_modules"은 기본적으로 아래의 모든 항목을 제외합니다 . 추가 "node_modules/@types/**/*.d.ts"하면 해당 제외를 재정의하고 tsc는 포함하려고 시도하지만 해당 유형은 이미 선언되어 있습니다.

tsconfig.json의 Typescript 문서를 확인하십시오 . “typeRoots” , “files”“include” / “exclude” 구성 옵션에 대해 자세히 설명합니다.


답변

나에게는 node_modules부모 디렉토리 프로젝트에 다음과 비슷한 폴더 가 있음이 밝혀졌습니다 .

node_modules
my-project
- node_modules

node_modules에 이전 버전이 @types/node설치 되었기 때문에 문제가 발생했습니다. 내 경우 해결책은 업데이트가 @types/node아니라 node_modules처음에 사용하지 않았기 때문에 제거하는 것이 었습니다.

실제로 node_modules다른 유형의 상위 디렉토리 에 있어야 하고 이것이 원하는 방식이면 typeRoots구체적으로 다음을 지정할 수 있습니다 .

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es6",
    "declaration": true,
    "outDir": "./dist",
    "typeRoots": ["./node_modules/@types/"]
  },
  "include": [
    "src/**/*"
  ]
}

이렇게하면 부모 node_modules가 유형을 검색하지 않습니다. 그렇지 않으면 https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types 에서 읽으십시오.

기본적으로 표시되는 모든 “@types”패키지가 컴파일에 포함됩니다. 포함하는 폴더의 node_modules / @ types에있는 패키지는 보이는 것으로 간주됩니다. 특히 ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/ 등의 패키지를 의미합니다.


답변

@Muhammad 빈 Yusrat 그의 의견에 말했듯이, 실행이 npm i @types/node@latest( npm i @types/node하지 작업을 수행!) 당신은 나를 위해 일한 9.에 각 업데이트 한 경우.

또한 ionic serve‘http : localhost : 8100 / favicon.ico’를 실행 한 후 또 다른 ionic 5 콘솔 오류를 제거 했습니다. 다음 콘텐츠 보안 정책을 위반했기 때문에 ‘http : localhost : 8100 / favicon.ico’이미지로드가 거부되었습니다. ….. ‘(아래 참조).

ionic serve 실행 후 ionic 5 오류

또 다른 ‘IteratorResult’오류는 “Spread Types”오류로 인해 발생했습니다. Typescript : 스프레드 유형은 객체 유형에서만 만들 수 있음을 참조하십시오 . 기본적으로 코드의 어딘가에서 이와 같은 확산 연산자를 사용했으며 return { id: doc.payload.id, ...doc.payload.data() };이를 변경해야 return { id: doc.payload.id, ...doc.payload.data() as {} }; 합니다.as {}


답변

@types/nodeAngular 프로젝트의 devDependencies에서 업그레이드 하십시오.

 npm i --save-dev @types/node

*** 아무 것도 변경하지 마십시오 node_modules***


답변

추가 "skipLibCheck": truecompilerOptionstsconfig.json에.

이것은 문제를 해결했습니다. 여기에서 확인