mihneadb / node-directory-tree

Convert a directory tree to a JS object.
MIT License
523 stars 106 forks source link

Fix TypeScript declaration #121

Closed rigwild closed 1 year ago

rigwild commented 1 year ago

When exporting some values coming from directory-tree, this TypeScript error is thrown:

src/example.ts:3:14 - error TS4023: Exported variable 'getTree' has or is using name 'IObj' from external module "/xxxxx/node_modules/.pnpm/directory-tree@3.4.0/node_modules/directory-tree/index" but cannot be named.

3 export const example = () => {
                ~~~~~~~

Found 1 error in src/example.ts:3

Code:

import * as dirTree from 'directory-tree'
// or if `esModuleInterop` is `true`:
// import dirTree from 'directory-tree'

export const example = () => {
  return dirTree('src')
}

tsconfig (error happens when emitting declarations):

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist/"
  },
  "include": ["src/*.ts"],
  "exclude": ["node_modules/**/*", "test/**/*"]
}

I replaced the IObj type with Record, which is equivalent but included in TypeScript. It fixes the issue.

mihneadb commented 1 year ago

Thanks!