Mopcho
Mopcho

Reputation: 11

Typescript declaration file not getting compiled properly

I have this lib.d.ts file

import { Users } from '@prisma/client';

declare global {
    namespace Express {
        interface User extends Users {}
    }
}

and I have this passport.ts in the same folder, where user is of type Express.User

passport.serializeUser((user, done) => {
    done(null, user.id);
});

the IDE doesnt complain about it, but when I run the app I get this error :

error TS2339: Property 'id' does not exist on type 'User'.

This is my tsconfig.json :

{
  "compilerOptions": {
    "target": "es2016",                                
    "lib": ["es6"],                                    
    "module": "commonjs",                              
    "rootDir": "src",                                  
    "resolveJsonModule": true,                         
    "allowJs": true,                                   
    "outDir": "build",                                 
    "esModuleInterop": true,                       
    "forceConsistentCasingInFileNames": true,          
    "strict": true,                                    
    "noImplicitAny": true,                             
    "skipLibCheck": true,                         
  }
}

and this is the path from tsconfig json to the file :

Why does that happen, ive been trying to fix this error for 2 days now.

EDIT : I fixed the error. It had to do with how I started my app.

Previously I started it with this script :

ts-node ./src/index.ts

adding the --files flag fixed it for me

ts-node --files ./src/index.ts

Here is an explanation : https://www.npmjs.com/package/ts-node#missing-types

Upvotes: 1

Views: 853

Answers (1)

Saurav Ghimire
Saurav Ghimire

Reputation: 540

The problem could be because you are trying to import at the top of the lib.d.ts file. I had a similar problem and starting the declaration on the first line of the file fixed my problem. Try this.

declare global {
    namespace Express {
        user: import("@prisma/client").Users
    }
}

Upvotes: 3

Related Questions