Node.js 如何在大型项目中的孤立文件上使用带有npm的本地TypeScript?

Node.js 如何在大型项目中的孤立文件上使用带有npm的本地TypeScript?,node.js,typescript,Node.js,Typescript,我有一个Expo TypeScript项目,其中包含一些scratch TypeScript文件,我想通过运行tsc(然后在发出的JS上运行node)来测试这些文件。但这样做会导致意外错误,并且无法报告合法的预期TS错误 我有一个(我相信是正确的)配置的TypeScript,它在本地安装了npm,如下所示 但是,当我尝试在TypeScript文件上使用tsc时,我会遇到意想不到的错误,例如执行 npx tsc somefile.ts 此外,tsc(至少在如上所述调用时)无法发出任何预期的错误或

我有一个Expo TypeScript项目,其中包含一些scratch TypeScript文件,我想通过运行
tsc
(然后在发出的JS上运行
node
)来测试这些文件。但这样做会导致意外错误,并且无法报告合法的预期TS错误

我有一个(我相信是正确的)配置的TypeScript,它在本地安装了npm,如下所示

但是,当我尝试在TypeScript文件上使用
tsc
时,我会遇到意想不到的错误,例如执行

npx tsc somefile.ts
此外,
tsc
(至少在如上所述调用时)无法发出任何预期的错误或编译器警告,例如,应该从中生成的错误或编译器警告

let foo: number = 5
foo = null
尽管如此,预期的
\u somefile.js
还是按预期生成并运行(忽略错误)

如何让
tsc
检测我的
.ts
文件中的错误,并避免它报告与我的项目安装相关的其他模块中的大量错误


npx tsc somefile.ts的输出,缩写为:

node_modules/@types/react-native/globals.d.ts:40:15 - error TS2300: Duplicate identifier 'FormData'.

40 declare class FormData {
                 ~~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:5353:11
    5353 interface FormData {
                   ~~~~~~~~
    'FormData' was also declared here.
  node_modules/typescript/lib/lib.dom.d.ts:5363:13
    5363 declare var FormData: {
                     ~~~~~~~~
    and here.

node_modules/@types/react-native/globals.d.ts:85:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'body' must be of type 'BodyInit', but here has type 'string | ArrayBuffer | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | Blob | FormData'.

85     body?: BodyInit_;
       ~~~~

  node_modules/typescript/lib/lib.dom.d.ts:1413:5
    1413     body?: BodyInit | null;
             ~~~~
    'body' was also declared here.

node_modules/@types/react-native/globals.d.ts:111:14 - error TS2300: Duplicate identifier 'RequestInfo'.

111 declare type RequestInfo = Request | string;
                 ~~~~~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:18568:6
    18568 type RequestInfo = Request | string;
               ~~~~~~~~~~~
    'RequestInfo' was also declared here.

node_modules/@types/react-native/globals.d.ts:130:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'Response' must be of type '{ new (body?: BodyInit, init?: ResponseInit): Response; prototype: Response; error(): Response; redirect(url: string, status?: number): Response; }', but here has type '{ new (body?: string | ArrayBuffer | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | Blob | FormData, init?: ResponseInit): Response; prototype: Response; error: () => Response; redirect: (url: string, status?: number) => Res...'.

130 declare var Response: {
                ~~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:12463:13
    12463 declare var Response: {
                      ~~~~~~~~
    'Response' was also declared here.

node_modules/@types/react-native/globals.d.ts:253:14 - error TS2300: Duplicate identifier 'XMLHttpRequestResponseType'.

253 declare type XMLHttpRequestResponseType = "" | "arraybuffer" | "blob" | "document" | "json" | "text";
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:18746:6
    18746 type XMLHttpRequestResponseType = "" | "arraybuffer" | "blob" | "document" | "json" | "text";
               ~~~~~~~~~~~~~~~~~~~~~~~~~~
    'XMLHttpRequestResponseType' was also declared here.

[...]

node_modules/typescript/lib/lib.dom.d.ts:18746:6 - error TS2300: Duplicate identifier 'XMLHttpRequestResponseType'.

18746 type XMLHttpRequestResponseType = "" | "arraybuffer" | "blob" | "document" | "json" | "text";
           ~~~~~~~~~~~~~~~~~~~~~~~~~~

  node_modules/@types/react-native/globals.d.ts:253:14
    253 declare type XMLHttpRequestResponseType = "" | "arraybuffer" | "blob" | "document" | "json" | "text";
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    'XMLHttpRequestResponseType' was also declared here.


Found 12 errors.
tsconfig.json的内容

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "jsx": "react-native",
    "lib": ["dom", "esnext"],
    "moduleResolution": "node", 
    "noEmit": true, 
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strict": true 
  }
}
  "dependencies": {
    "expo": "^34.0.4",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "react-native-web": "^0.11.5",
    "native-base": "^2.13.4",
    "redux": "^4.0.4",
    "react-redux": "^7.1.0"
  },
project.json
中的依赖项:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "jsx": "react-native",
    "lib": ["dom", "esnext"],
    "moduleResolution": "node", 
    "noEmit": true, 
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strict": true 
  }
}
  "dependencies": {
    "expo": "^34.0.4",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "react-native-web": "^0.11.5",
    "native-base": "^2.13.4",
    "redux": "^4.0.4",
    "react-redux": "^7.1.0"
  },

出现这些错误是因为React Native和标准
dom
模块定义了相同的类型


删除你不使用的那些文件。

我也有一个关于从WebStorm“运行”TypeScript文件的支持。那么我的
project.json
是否应该删除一些依赖项(添加do OP)呢?我该怎么做。例如,从我的
项目中删除
react
。json
仍然会产生错误。此错误来自TypeScript编译器,而不是npm。您需要将其从
tsconfig.json
中的
lib
中删除。