line / line-blockchain-developers-sdk-js

Javascript library for line blockchain developers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScriptのコンパイルエラーにつきまして

hajime001 opened this issue · comments

Default information

  • lbd-sdk-js version: 1.4.8
  • nodeJs version: 16.13.0
  • next.js: 13.3.1
  • compilerOptions.target: "ES2015"

Description of the bug

Next.jsを使っていてnpmで本ライブラリをインストールして「npm run build」を実行すると下記のlintエラーが出ました。

ここだけならプルリクを送ろうかと思ったのですが他にもLintエラーが出てる箇所があり、
仕様を把握しきれてない状態で下手にいじらないほうが良いと思ったので、取り急ぎ報告させていただきます。

Error logs

info  - Linting and checking validity of types ...Failed to compile.

./node_modules/@line/lbd-sdk-js/lib/http-client-base.ts:167:54
Type error: 'err.response' is possibly 'undefined'.

  165 |       );
  166 |     } else if (err.code) {
> 167 |       return new RequestError(err.message, err.code, err.response.data.statusMessage || "", err);
      |                                                      ^
  168 |     } else if (err.config) {
  169 |       // unknown, but from axios
  170 |       return new ReadError(err);

LintではなくTypeScriptのコンパイルエラーだったのでタイトルを変更しました。

tsconfig.jsonのstrictを「false」にすればエラーは回避できるようです。
ただ好ましくはないと思われるので、Issue自体は残しておきます。
対応不要との判断であればクローズください。

Thank you reporting and sorry for late response, but I appreciate if you provide us a sort of sample repository to reproduce the issue.

@hajime001 I created simple app with nextjs and lbd-sdk-js, but I didn't meet the error. so I appreciate if you take a look at the sample app and let us know what is different from your codes.
https://github.com/ryukato/nextjs-lbd-sdk-js-sample

Thank you for your reply. I have checked the repository.
With the code below, I get an error with "npm run build".
Am I doing import wrong?
The IDE automatically imported it this way.

src/pages/api/lbdApi.ts

import {HttpClient} from '@line/lbd-sdk-js/lib/http-client-base';

const config = {
    baseUrl: "https://test.com",
    apiKey: "test",
    apiSecret: "test"
};
const { baseUrl, apiKey, apiSecret } = config;
const httpLbdClient = new HttpClient(baseUrl, apiKey, apiSecret);
export default httpLbdClient;

@hajime001 Can you share your tsconfig.json file with us so we can better understand the issue?

My tsconifig.json is below

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2015",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

Also occurs in the following file

https://github.com/ryukato/nextjs-lbd-sdk-js-sample/blob/main/tsconfig.json

Thank you. Your tsconifig.json file seems to be fine. To resolve the issue, we are planning to apply "strict": true option to the next release. After that, the issue will not appear.

@yeojin-dev Thank you.
By the way, I don't have any error from npm run build even though I applied same tsconfig.json with yours.

@hajime001 I appreciate if there is anything I missed or mis-understanding. :)

npm run build

> my-app@0.1.0 build
> next build

info  - Linting and checking validity of types
info  - Creating an optimized production build
info  - Compiled successfully
info  - Collecting page data
info  - Generating static pages (3/3)
info  - Finalizing page optimization

Route (pages)                              Size     First Load JS
┌ ○ /                                      4.77 kB         632 kB
├   └ css/06b915eca0c341b5.css             1.73 kB
├   /_app                                  0 B             627 kB
├ ○ /404                                   182 B           628 kB
├ λ /api/hello                             0 B             627 kB
└ λ /api/lbdApi                            0 B             627 kB
+ First Load JS shared by all              628 kB
  ├ chunks/framework-2c79e2a64abdb08b.js   45.2 kB
  ├ chunks/main-17a9a24315ee9390.js        27.8 kB
  ├ chunks/pages/_app-ff490169bd6e3bcb.js  554 kB
  ├ chunks/webpack-38cee4c0e358b1a3.js     862 B
  └ css/876d048b5dab7c28.css               706 B

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
○  (Static)  automatically rendered as static HTML (uses no initial props)

I checked that with the sample

@ryukato
#131 (comment)

Did you use the above code? The import method is different from the sample.

I don't think the error is caused by a difference in tsconfig.json, but by a difference in the way the code is checked by the way it is imported.

@hajime001 san, Oh.. I see.
then could you update the codes like below and try again?

import { HttpClient } from '@line/lbd-sdk-js';

const config = {
    baseUrl: "https://test.com",
    apiKey: "test",
    apiSecret: "test"
};
const { baseUrl, apiKey, apiSecret } = config;
const httpLbdClient = new HttpClient(baseUrl, apiKey, apiSecret);
export default httpLbdClient;

because it can be referenced and used by node_modules/@line/lbd-sdk-js/dist/http-client-base.d.ts.

@ryukato
I understand that point as I have seen your samples.
I think it is possible to state explicitly that the library does not support imports other than '@line/lbd-sdk-js'.
However, my IDE automatically generates import statements in the form 'node_modules/@line/lbd-sdk-js/dist/http-client-base.d.ts.' and I think the TypeScript diagnosis itself is valid, so I think it would be better to fix it.
However, I am not the developer of the library, so I will leave that decision to those responsible.

Sure why not, we will look into that and let you know.
Thanks again your reporting.

This is fixed by #132