Search code examples
node.jstypescripttypesdefinition

typescript .d.ts file not recognized by typescript but vsc


Update:

it isnt a typescript, but ts-node "issue"

i answered to myself so if ure still interested in the "issue" - read further :)

sorry for the confusion


maybe I have a TOTALLY wrong idea of the index.d.ts file.. but my idea was, that i can declare types within it and they are then "globally defined", so i dont have to redeclare them everywhere i use them (file a.ts and b.ts)

structure:

root
  src
    a.ts
    b.ts
  node_modules
  tsconfig.json
  index.d.ts
  package.json

for example i have a index.d.ts file:

type XYZType = { test: string }

in my ./src/a.ts I have:

const x: XYZType = { test: 'hello' }
console.log(x)

in my ./src/b.ts I have:

const x: XYZType = { test: 'world' }
console.log(x)

however vsc (Visual Studio Code) doesn't complain about missing XYZType Type.. but when I try to let typescript compile, it does...:

error TS2304: Cannot find name 'XYZType'.

and points to file a.ts and b.ts

so.. to be obvious.. it looks like I do something wrong..

i followed the little example from levelup link

so my tsconfig.json looks like

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/*", "src/types/*"]
    }
  },
  "include": ["src/**/*"]
}

i'm using:

    "ts-node": "^9.0.0",
    "typescript": "^4.0.3"

i found a lot of stack overflow requests with the same title.. but totally different content (own package declarations?) so this is why it confuses me and i have the feeling im totally wrong with the approach im looking for

hope anybody can help and explain whats wrong :/

thanks in advance :)


Solution

  • ok wow - it isnt a typescript, but an ts-node issue

    in the example link the package.json script "dev" is setup like

    "scripts": { 
      "dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts", 
    } 
    

    but has to be:

    "scripts": { 
      "dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' --files src/index.ts", 
    } 
    

    ('--files' has to be added)

    sorry for the confusion but hopefully it helps someone running in the same issue :)