Danfo-JS与TypeScript类型冲突编译报错求助
问题
我正在尝试编译使用danfojs-node的TypeScript代码,当前配置如下:
package.json
{ "dependencies": { "danfojs-node": "^1.1.2", "typescript": "^5.2.2" } }
tsconfig.json
{ "compilerOptions": { "target": "es5", "module": "commonjs", "declaration": true, "outDir": "./dist", "strict": true }, "include": ["src"], "exclude": ["node_modules", "**/__tests__/*"], "skipLibCheck": true }
运行tsc编译时出现7个类型冲突错误:
../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:23073:11 - error TS2430: Interface 'WebGL2RenderingContext' incorrectly extends interface 'WebGL2RenderingContextBase'. Types of property 'READ_BUFFER' are incompatible. Type 'number' is not assignable to type '3074'. 23073 interface WebGL2RenderingContext extends WebGL2RenderingContextBase, WebGL2RenderingContextOverloads, WebGLRenderingContextBase { ~~~~~~~~~~~~~~~~~~~~~~ ../node_modules/@types/webgl-ext/index.d.ts:53:11 - error TS2717: Subsequent property declarations must have the same type. Property 'RGBA16F_EXT' must be of type '34842', but here has type 'number'. 53 readonly RGBA16F_EXT: number; ~~~~~~~~~~~ ../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:7477:14 7477 readonly RGBA16F_EXT: 0x881A; ~~~~~~~~~~~ 'RGBA16F_EXT' was also declared here. ../node_modules/@types/webgl-ext/index.d.ts:54:11 - error TS2717: Subsequent property declarations must have the same type. Property 'RGB16F_EXT' must be of type '34843', but here has type 'number'. 54 readonly RGB16F_EXT: number; ~~~~~~~~~~ ../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:7478:14 7478 readonly RGB16F_EXT: 0x881B; ~~~~~~~~~~ 'RGB16F_EXT' was also declared here. ../node_modules/@types/webgl-ext/index.d.ts:55:11 - error TS2717: Subsequent property declarations must have the same type. Property 'FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT' must be of type '33297', but here has type 'number'. 55 readonly FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT: number; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:7479:14 7479 readonly FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT: 0x8211; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT' was also declared here. ../node_modules/@types/webgl-ext/index.d.ts:56:11 - error TS2717: Subsequent property declarations must have the same type. Property 'UNSIGNED_NORMALIZED_EXT' must be of type '35863', but here has type 'number'. 56 readonly UNSIGNED_NORMALIZED_EXT: number; ~~~~~~~~~~~~~~~~~~~~~~~ ../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:7480:14 7480 readonly UNSIGNED_NORMALIZED_EXT: 0x8C17; ~~~~~~~~~~~~~~~~~~~~~~~ 'UNSIGNED_NORMALIZED_EXT' was also declared here. ../node_modules/@types/webgl-ext/index.d.ts:74:11 - error TS2717: Subsequent property declarations must have the same type. Property 'COMPRESSED_RGB_ETC1_WEBGL' must be of type '36196', but here has type 'number'. 74 readonly COMPRESSED_RGB_ETC1_WEBGL: number; ~~~~~~~~~~~~~~~~~~~~~~~~~ ../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:22895:14 22895 readonly COMPRESSED_RGB_ETC1_WEBGL: 0x8D64; ~~~~~~~~~~~~~~~~~~~~~~~~~ 'COMPRESSED_RGB_ETC1_WEBGL' was also declared here. ../node_modules/@types/webgl2/index.d.ts:582:13 - error TS2403: Subsequent variable declarations must have the same type. Variable 'WebGL2RenderingContext' must be of type '{ new (): WebGL2RenderingContext; prototype: WebGL2RenderingContext; readonly READ_BUFFER: 3074; readonly UNPACK_ROW_LENGTH: 3314; ... 556 more ...; readonly BROWSER_DEFAULT_WEBGL: 37444; }', but here has type '{ new (): WebGL2RenderingContext; prototype: WebGL2RenderingContext; readonly ACTIVE_ATTRIBUTES: number; readonly ACTIVE_TEXTURE: number; ... 556 more ...; readonly MAX_CLIENT_WAIT_TIMEOUT_WEBGL: number; }'. 582 declare var WebGL2RenderingContext: { ~~~~~~~~~~~~~~~~~~~~~~ ../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:23076:13 23076 declare var WebGL2RenderingContext: { ~~~~~~~~~~~~~~~~~~~~~~ 'WebGL2RenderingContext' was also declared here. Found 7 errors in 3 files. Errors Files 1 ../../.nvm/versions/node/v18.17.0/lib/node_modules/typescript/lib/lib.dom.d.ts:23073 5 ../node_modules/@types/webgl-ext/index.d.ts:53 1 ../node_modules/@types/webgl2/index.d.ts:582
解决方案
这些错误是TypeScript内置的lib.dom.d.ts与第三方@types/webgl-ext、@types/webgl2类型定义重复冲突导致的,danfojs-node的依赖包间接引入了这些重复类型。可通过以下步骤解决:
卸载重复的WebGL类型依赖
由于TypeScript标准库已包含完整的WebGL类型定义,直接卸载第三方类型包:npm uninstall @types/webgl-ext @types/webgl2调整tsconfig.json的标准库配置
明确指定Node环境所需的标准库,避免自动引入多余的DOM类型:
修改tsconfig.json的compilerOptions部分,添加lib字段:"compilerOptions": { "target": "es5", "module": "commonjs", "declaration": true, "outDir": "./dist", "strict": true, "lib": ["es2015"], "skipLibCheck": true }清理并重新安装依赖
若卸载后仍有残留问题,清理依赖目录和锁文件后重新安装:rm -rf node_modules package-lock.json npm install验证编译
再次运行tsc即可完成正常编译。
内容的提问来源于stack exchange,提问作者Gabriele
相关产品推荐
相关产品推荐

