hujiulong / gcoord

地理坐标系转换工具
MIT License
3.08k stars 383 forks source link

typescript 引用报错 #214

Closed zzswang closed 1 year ago

zzswang commented 1 year ago

在 ts 工程中,这么写会报错

import gcoord from 'gcoord';

编译出来的代码会发现 gcoord 找不到

Cannot read properties of undefined (reading 'transform')

必须改成这样才行

import * as gcoord from 'gcoord';

改成这样后,语法提示又出错了。比较别扭,不知道是哪里的问题。

zzswang commented 1 year ago

是不是和 rollup 打包的方式有关系?

ts 的标准我一直没搞明白

hujiulong commented 1 year ago

这个在 tsconfig 里设置 "allowSyntheticDefaultImports": true 就好了

zzswang commented 1 year ago

allowSyntheticDefaultImports

谢谢,这个开关开上了,但还是一样的。目前的 tsconfig 配置,是在 nestjs 自动生成的基础上稍加改动的。

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "src/*": ["src/*"],
      "tests/*": ["__tests__/*"]
    }
  },
  "include": ["src", "test", "typings"]
}

代码中避免语法错误提示,是这么写的

import * as gcoord from 'gcoord';

....

// @ts-ignore
const [lng, lat] = gcoord.transform(
  [record.body.location.lng, record.body.location.lat],
  // @ts-ignore
  gcoord.WGS84,
  // @ts-ignore
  gcoord.GCJ02
);
hujiulong commented 1 year ago

我随便找了个项目试了下,没问题 image

项目的tsconfig配置:

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "inlineSourceMap":true,
    "noImplicitThis": true,
    "noUnusedLocals": false,
    "stripInternal": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "strictBindCallApply": true,
    "pretty": true,
    "declaration": true,
    "forceConsistentCasingInFileNames": true,
    "typeRoots": [ "./typings", "./node_modules/@types"],
    "outDir": "dist"
  },
  "exclude": [
    "dist",
    "node_modules",
    "test"
  ]
}
hujiulong commented 1 year ago

你再研究下ts配置和构建配置吧,看起来和 gcoord 本身没关系,这个我 close 了

zzswang commented 1 year ago

多谢

语法提示是没问题的。编译运行会报错,应该和 gcoord 的 rollup 配置有关系。

hujiulong commented 1 year ago

我新建了一个 nestjs 的项目试了下,需要在 tsconfig 中配置 "esModuleInterop": true,tsconfig 的配置如下:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "esModuleInterop": true,
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}

这样类型提示和运行都没问题了。

这应该是不同模块规范冲突的问题,我近期修复一下

zzswang commented 1 year ago

"esModuleInterop": true

解决问题了