Swatinem / rollup-plugin-dts

A rollup plugin to generate .d.ts rollup files for your typescript project
GNU Lesser General Public License v3.0
816 stars 71 forks source link

Unexpected error when using lodash #304

Closed 9romise closed 6 months ago

9romise commented 7 months ago

I got a type error when using lodash. If you change it to typeof, it works.

Checklist

Code Snipped

// demo.ts
import { isBoolean, isFunction } from 'lodash-es';

type Option = boolean | ScrollToOptions | (() => boolean | ScrollToOptions);

export function demo(option: Option = true) {
  if (option === false)
    return;

  let options: ScrollToOptions = { left: 0, top: 0 };

  if (isFunction(option)) {
    const res = option() ?? true;

    if (res === false)
      return;
    else if (!isBoolean(res))
      options = res;
  }
  else if (!isBoolean(option)) {
    options = option;
  }

  return options;
}
// rollup.config.ts
import type { RollupOptions } from 'rollup';
import dts from 'rollup-plugin-dts';

const input = ['src/index.ts'];

export default [
  {
    input,
    output: [
      { file: 'dist/types/index.d.ts' },
    ],
    plugins: [dts()],
  },
] as RollupOptions;

Error Message

src/index.ts(15,17): error TS2349: This expression is not callable.
  Not all constituents of type 'true | ScrollToOptions | (() => boolean | ScrollToOptions)' are callable.
    Type 'true' has no call signatures.
src/index.ts(23,5): error TS2322: Type 'true | ScrollToOptions | (() => boolean | ScrollToOptions)' is not assignable to type 'ScrollToOptions'.
  Type 'true' has no properties in common with type 'ScrollToOptions'.

[!] (plugin dts) RollupError: [plugin dts] src/index.ts: Failed to compile. Check the logs above.
src/index.ts
    at getRollupError (D:\Projects\swiper-extra-modules\node_modules\.pnpm\rollup@4.14.1\node_modules\rollup\dist\shared\parseAst.js:282:41)
    at Object.error (D:\Projects\swiper-extra-modules\node_modules\.pnpm\rollup@4.14.1\node_modules\rollup\dist\shared\parseAst.js:278:42)
    at Object.error (D:\Projects\swiper-extra-modules\node_modules\.pnpm\rollup@4.14.1\node_modules\rollup\dist\shared\rollup.js:804:32)
    at Object.error (D:\Projects\swiper-extra-modules\node_modules\.pnpm\rollup@4.14.1\node_modules\rollup\dist\shared\rollup.js:19612:42)
    at generateDtsFromTs (file:///D:/Projects/swiper-extra-modules/node_modules/.pnpm/rollup-plugin-dts@6.1.0_rollup@4.14.1_typescript@5.4.4/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1833:30)
    at Object.transform (file:///D:/Projects/swiper-extra-modules/node_modules/.pnpm/rollup-plugin-dts@6.1.0_rollup@4.14.1_typescript@5.4.4/node_modules/rollup-plugin-dts/dist/rollup-plugin-dts.mjs:1842:38)
    at D:\Projects\swiper-extra-modules\node_modules\.pnpm\rollup@4.14.1\node_modules\rollup\dist\shared\rollup.js:989:40