I am currently working on implementing correct User typing with passport.js.
Within the Express namespace, Passport defines the User interface as an empty interface. You can view the details here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/22f314360cf1f8c273e9985748519cb07984dc79/types/passport/index.d.ts#L20
By default, code like the example below will result in the error "Property 'id' does not exist on type 'User'":
passport.serializeUser(function(user, cb) {
process.nextTick(function() {
cb(null, user.id);
});
});
This error is expected.
To resolve this issue, I utilized type merging by creating a .d.ts file with the following content:
// express_augmentation.d.ts
declare namespace Express {
interface User {
id: string
}
}
This solution worked well for me.
However, if you prefer to use your own User class that includes an id property and potentially other properties, you may consider the following approach:
// express_augmentation.d.ts
declare namespace Express {
import MyUser from '../models/user';
interface User extends MyUser {}
}
Unfortunately, this method did not yield the desired outcome for me. I'm unsure about what I might be missing. Any suggestions?