Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript 引用报错 #214

Closed
zzswang opened this issue Jan 8, 2023 · 8 comments
Closed

typescript 引用报错 #214

zzswang opened this issue Jan 8, 2023 · 8 comments

Comments

@zzswang
Copy link

zzswang commented Jan 8, 2023

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

import gcoord from 'gcoord';

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

Cannot read properties of undefined (reading 'transform')

必须改成这样才行

import * as gcoord from 'gcoord';

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

@zzswang
Copy link
Author

zzswang commented Jan 8, 2023

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

ts 的标准我一直没搞明白

@hujiulong
Copy link
Owner

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

@zzswang
Copy link
Author

zzswang commented Jan 8, 2023

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
Copy link
Owner

hujiulong commented Jan 12, 2023

我随便找了个项目试了下,没问题
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
Copy link
Owner

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

@zzswang
Copy link
Author

zzswang commented Jan 14, 2023

多谢

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

@hujiulong
Copy link
Owner

hujiulong commented Jan 16, 2023

我新建了一个 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
Copy link
Author

zzswang commented Jan 16, 2023

"esModuleInterop": true

解决问题了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants