meltingice / psd.js

A Photoshop PSD file parser for NodeJS and browsers
MIT License
2.75k stars 391 forks source link

How do I load "psd.d.ts"? #277

Closed Jamu2800 closed 1 year ago

Jamu2800 commented 1 year ago

I made a prototype of "psd.d.ts". But it doesn't seem to load correctly into typescript. I need your help.

errors

"C:\Program Files\nodejs\npm.cmd" run start

> type_test@1.0.0 start
> ts-node src/typescript/index.ts

D:\program\type_test\node_modules\ts-node\src\index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/typescript/index.ts:1:26 - error TS7016: Could not find a declaration file for module 'psd'. 'D:/program/type_test/node_modules/psd/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/psd` if it exists or add a new declaration (.d.ts) file containing `declare module 'psd';`

1 import { fromFile } from "psd"
                           ~~~~~

    at createTSError (D:\program\type_test\node_modules\ts-node\src\index.ts:859:12)
    at reportTSError (D:\program\type_test\node_modules\ts-node\src\index.ts:863:19)
    at getOutput (D:\program\type_test\node_modules\ts-node\src\index.ts:1077:36)
    at Object.compile (D:\program\type_test\node_modules\ts-node\src\index.ts:1433:41)
    at Module.m._compile (D:\program\type_test\node_modules\ts-node\src\index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Object.require.extensions.<computed> [as .ts] (D:\program\type_test\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
  diagnosticCodes: [ 7016 ]
}

my project

project dir image psd.d.ts

export declare module "psd" {

  function fromFile(path: string): PSD

  type PSD = {
    parse(): boolean
    tree(): TreePSD
  }

  type TreePSD = {
    get(key: string): any
    export(): ExportPSD
    root(): TreePSD
    isRoot(): boolean
    children(): TreePSD[]
    hasChildren(): boolean
    childless(): boolean
    ancestors(): TreePSD
    siblings(): TreePSD[]
    nextSibling(): TreePSD | undefined
    prevSibling(): TreePSD | undefined
    hasSiblings(): boolean
    onlyChild(): boolean
    descendants(): TreePSD[]
    subtree(): TreePSD[]
    depth(): number
    path(): string
  }

  type ExportPSD = {}

  export {
    fromFile,
    PSD,
    TreePSD
  }
}

package.json

{
  "name": "type_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "ts-node src/typescript/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "psd": "^3.4.0",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4"
  }
}

tsconfig.json

{
  "compilerOptions": {

    /* Language and Environment */
    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */

    /* Modules */
    "rootDir": "./src",                                  /* Specify the root folder within your source files. */
    "baseUrl": "./src",                                  /* Specify the base directory to resolve non-relative module names. */
    "typeRoots": [
      "./src/typescript/@types"
    ],                                  /* Specify multiple folders that act like './node_modules/@types'. */

    /* Interop Constraints */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */

    /* Completeness */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}
pastelmind commented 1 year ago

This is a ts-node problem.

ts-node does not load all .d.ts files by default. You need to:

  1. Use --files option to make ts-node load all files
  2. If the above doesn't work, manually add the .d.ts file to the include section of your tsconfig.json.

Reference: https://typestrong.org/ts-node/docs/troubleshooting#missing-types

Jamu2800 commented 1 year ago

This problem had two causes. One is a problem with ts-node. The ts-node problem is

Use --files option to make ts-node load all files

The problem could be solved by using. Thank you very much.

The second problem was in the d.ts file. It seems that "export" does not load them. Fixed index.d.ts

declare module 'psd' {

  function fromFile(path: string): PSD

  type PSD = {
    parse(): boolean
    tree(): TreePSD
  }

  type TreePSD = {
    get(key: string): any
    export(): ExportPSD
    root(): TreePSD
    isRoot(): boolean
    children(): TreePSD[]
    hasChildren(): boolean
    childless(): boolean
    ancestors(): TreePSD
    siblings(): TreePSD[]
    nextSibling(): TreePSD | undefined
    prevSibling(): TreePSD | undefined
    hasSiblings(): boolean
    onlyChild(): boolean
    descendants(): TreePSD[]
    subtree(): TreePSD[]
    depth(): number
    path(): string
  }

  type ExportPSD = {}

  export {
    fromFile,
    PSD,
    TreePSD
  }
}