【发布时间】:2021-11-25 13:03:10
【问题描述】:
我创建了一个自定义库,其中包含我在各种 React Native 项目中使用的所有 API 请求。该库在其 package.json 中有以下依赖项
"dependencies": {
"@types/node-fetch": "^2.5.10",
"typescript": "^4.3.5"
}
它还包括一个安装后脚本,用于使用tsc 构建库,因为这是一个 Typescript 项目。
在我的 React Native 项目中,我通过其 GitHub 存储库安装了该库,当我的安装后脚本运行时,我收到以下“重复标识符”错误:
npm ERR! command sh -c tsc
npm ERR! ../@types/node/globals.d.ts(47,11): error TS2300: Duplicate identifier 'AbortController'.
npm ERR! ../@types/node/globals.d.ts(60,11): error TS2300: Duplicate identifier 'AbortSignal'.
npm ERR! ../@types/node/globals.d.ts(67,13): error TS2300: Duplicate identifier 'AbortController'.
npm ERR! ../@types/node/globals.d.ts(72,13): error TS2300: Duplicate identifier 'AbortSignal'.
npm ERR! ../@types/react-native/globals.d.ts(49,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'Blob' must be of type '{ new (blobParts?: BlobPart[] | undefined, options?: BlobPropertyBag | undefined): Blob; prototype: Blob; }', but here has type '{ new (blobParts?: (string | Blob)[] | undefined, options?: BlobOptions | undefined): Blob; prototype: Blob; }'.
npm ERR! ../@types/react-native/globals.d.ts(54,15): error TS2300: Duplicate identifier 'FormData'.
npm ERR! ../@types/react-native/globals.d.ts(110,5): error TS2717: Subsequent property declarations must have the same type. Property 'body' must be of type 'BodyInit | null | undefined', but here has type 'BodyInit_ | undefined'.
npm ERR! ../@types/react-native/globals.d.ts(118,5): error TS2717: Subsequent property declarations must have the same type. Property 'window' must be of type 'null | undefined', but here has type 'any'.
npm ERR! ../@types/react-native/globals.d.ts(119,5): error TS2717: Subsequent property declarations must have the same type. Property 'signal' must be of type 'AbortSignal | null | undefined', but here has type 'AbortSignal | undefined'.
npm ERR! ../@types/react-native/globals.d.ts(137,14): error TS2300: Duplicate identifier 'RequestInfo'.
npm ERR! ../@types/react-native/globals.d.ts(156,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'Response' must be of type '{ new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response; prototype: Response; error(): Response; redirect(url: string | URL, status?: number | undefined): Response; }', but here has type '{ new (body?: BodyInit_ | undefined, init?: ResponseInit | undefined): Response; prototype: Response; error: () => Response; redirect: (url: string, status?: number | undefined) => Response; }'.
npm ERR! ../@types/react-native/globals.d.ts(233,5): error TS2717: Subsequent property declarations must have the same type. Property 'abort' must be of type 'ProgressEvent<XMLHttpRequestEventTarget>', but here has type 'ProgressEvent<EventTarget>'.
npm ERR! ../@types/react-native/globals.d.ts(234,5): error TS2717: Subsequent property declarations must have the same type. Property 'error' must be of type 'ProgressEvent<XMLHttpRequestEventTarget>', but here has type 'ProgressEvent<EventTarget>'.
npm ERR! ../@types/react-native/globals.d.ts(235,5): error TS2717: Subsequent property declarations must have the same type. Property 'load' must be of type 'ProgressEvent<XMLHttpRequestEventTarget>', but here has type 'ProgressEvent<EventTarget>'.
npm ERR! ../@types/react-native/globals.d.ts(236,5): error TS2717: Subsequent property declarations must have the same type. Property 'loadend' must be of type 'ProgressEvent<XMLHttpRequestEventTarget>', but here has type 'ProgressEvent<EventTarget>'.
npm ERR! ../@types/react-native/globals.d.ts(237,5): error TS2717: Subsequent property declarations must have the same type. Property 'loadstart' must be of type 'ProgressEvent<XMLHttpRequestEventTarget>', but here has type 'ProgressEvent<EventTarget>'.
npm ERR! ../@types/react-native/globals.d.ts(238,5): error TS2717: Subsequent property declarations must have the same type. Property 'progress' must be of type 'ProgressEvent<XMLHttpRequestEventTarget>', but here has type 'ProgressEvent<EventTarget>'.
npm ERR! ../@types/react-native/globals.d.ts(239,5): error TS2717: Subsequent property declarations must have the same type. Property 'timeout' must be of type 'ProgressEvent<XMLHttpRequestEventTarget>', but here has type 'ProgressEvent<EventTarget>'.
npm ERR! ../@types/react-native/globals.d.ts(280,14): error TS2300: Duplicate identifier 'XMLHttpRequestResponseType'.
npm ERR! ../@types/react-native/globals.d.ts(287,15): error TS2300: Duplicate identifier 'URL'.
npm ERR! ../@types/react-native/globals.d.ts(312,15): error TS2300: Duplicate identifier 'URLSearchParams'.
npm ERR! ../@types/react-native/globals.d.ts(356,5): error TS2717: Subsequent property declarations must have the same type. Property 'onopen' must be of type '((this: WebSocket, ev: Event) => any) | null', but here has type '(() => void) | null'.
npm ERR! ../@types/react-native/globals.d.ts(357,5): error TS2717: Subsequent property declarations must have the same type. Property 'onmessage' must be of type '((this: WebSocket, ev: MessageEvent<any>) => any) | null', but here has type '((event: WebSocketMessageEvent) => void) | null'.
npm ERR! ../@types/react-native/globals.d.ts(358,5): error TS2717: Subsequent property declarations must have the same type. Property 'onerror' must be of type '((this: WebSocket, ev: Event) => any) | null', but here has type '((event: WebSocketErrorEvent) => void) | null'.
npm ERR! ../@types/react-native/globals.d.ts(359,5): error TS2717: Subsequent property declarations must have the same type. Property 'onclose' must be of type '((this: WebSocket, ev: CloseEvent) => any) | null', but here has type '((event: WebSocketCloseEvent) => void) | null'.
npm ERR! ../@types/react-native/globals.d.ts(360,5): error TS2717: Subsequent property declarations must have the same type. Property 'addEventListener' must be of type '{ <K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | AddEventListenerOptions | undefined): void; (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | ... 1 more ... | undefined): void; }', but here has type 'WebsocketEventListener'.
npm ERR! ../@types/react-native/globals.d.ts(361,5): error TS2717: Subsequent property declarations must have the same type. Property 'removeEventListener' must be of type '{ <K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions | undefined): void; (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | ... 1 more ... | undefined): void; }', but here has type 'WebsocketEventListener'.
npm ERR! ../@types/react-native/globals.d.ts(364,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'WebSocket' must be of type '{ new (url: string | URL, protocols?: string | string[] | undefined): WebSocket; prototype: WebSocket; readonly CLOSED: number; readonly CLOSING: number; readonly CONNECTING: number; readonly OPEN: number; }', but here has type '{ new (uri: string, protocols?: string | string[] | null | undefined, options?: { [optionName: string]: any; headers: { [headerName: string]: string; }; } | null | undefined): WebSocket; ... 4 more ...; readonly OPEN: number; }'.
npm ERR! ../@types/react-native/globals.d.ts(388,15): error TS2300: Duplicate identifier 'AbortSignal'.
npm ERR! ../@types/react-native/globals.d.ts(388,15): error TS2420: Class 'AbortSignal' incorrectly implements interface 'EventTarget'.
npm ERR! Property 'dispatchEvent' is missing in type 'AbortSignal' but required in type 'EventTarget'.
npm ERR! ../@types/react-native/globals.d.ts(411,15): error TS2300: Duplicate identifier 'AbortController'.
npm ERR! ../@types/react-native/globals.d.ts(436,14): error TS2717: Subsequent property declarations must have the same type. Property 'error' must be of type 'DOMException | null', but here has type 'Error | null'.
npm ERR! ../@types/react-native/globals.d.ts(444,14): error TS2717: Subsequent property declarations must have the same type. Property 'result' must be of type 'string | ArrayBuffer | null', but here has type 'string | ArrayBuffer'.
npm ERR! ../@types/react-test-renderer/node_modules/@types/react/index.d.ts(3065,14): error TS2300: Duplicate identifier 'LibraryManagedAttributes'.
npm ERR! ../@types/react-test-renderer/node_modules/@types/react/index.d.ts(3078,13): error TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type 'DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>', but here has type 'DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>'.
npm ERR! ../@types/react-test-renderer/node_modules/@types/react/index.d.ts(3079,13): error TS2717: Subsequent property declarations must have the same type. Property 'abbr' must be of type 'DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>', but here has type 'DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>'.
...
npm ERR! ../@types/react-test-renderer/node_modules/@types/react/index.d.ts(3254,13): error TS2717: Subsequent property declarations must have the same type. Property 'use' must be of type 'SVGProps<SVGUseElement>', but here has type 'SVGProps<SVGUseElement>'.
npm ERR! ../@types/react-test-renderer/node_modules/@types/react/index.d.ts(3255,13): error TS2717: Subsequent property declarations must have the same type. Property 'view' must be of type 'SVGProps<SVGViewElement>', but here has type 'SVGProps<SVGViewElement>'.
npm ERR! ../@types/react/index.d.ts(3068,14): error TS2300: Duplicate identifier 'LibraryManagedAttributes'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(1866,11): error TS2300: Duplicate identifier 'AbortController'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(1873,13): error TS2300: Duplicate identifier 'AbortController'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(1883,11): error TS2300: Duplicate identifier 'AbortSignal'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(1893,13): error TS2300: Duplicate identifier 'AbortSignal'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(5199,11): error TS2300: Duplicate identifier 'FormData'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(5209,13): error TS2300: Duplicate identifier 'FormData'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(13848,11): error TS2300: Duplicate identifier 'URL'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(13865,13): error TS2300: Duplicate identifier 'URL'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(13875,11): error TS2300: Duplicate identifier 'URLSearchParams'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(13894,13): error TS2300: Duplicate identifier 'URLSearchParams'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(17639,6): error TS2300: Duplicate identifier 'RequestInfo'.
npm ERR! node_modules/typescript/lib/lib.dom.d.ts(17791,6): error TS2300: Duplicate identifier 'XMLHttpRequestResponseType'.
npm ERR! src/services/HttpService.ts(15,31): error TS7006: Parameter 'value' implicitly has an 'any' type.
npm ERR! src/services/HttpService.ts(15,38): error TS7006: Parameter 'name' implicitly has an 'any' type.
我认为这是因为我在我的库以及 React Native 项目中安装了 node-fetch 和 typescript 的类型,但我不知道如何解决这个问题。
可能有帮助的信息:我还有一个 Angular 项目,我在其中成功安装了库。所以它肯定和 React Native 项目特别有关。
【问题讨论】:
-
您能否也显示您的安装后脚本?
-
这只是
tsc- 它只是应该获取javascript代码和它的类型。它正在一个 Angular 项目中工作。 -
我怀疑它试图编译太多。看起来它是一个后端库(因为它使用 node-fetch),我认为它不应该关心 react-native 的类型。
-
对不起,我在原来的问题中使用了错误的
tsc。我现在使用了库本身的tsc并更新了我的问题中的堆栈跟踪。但我认为你的理论可能是正确的,你知道我该如何解决吗? -
我认为你应该使用 tsconfig.json 和/或命令行选项来限制它需要编译的内容。现在它似乎编译了它可以找到的每个 .ts 文件。
标签: typescript react-native tsc