Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Error when build: Property 'xxx' does not exist on type 'User'. #221

Open
nguyentranchung opened this issue Aug 22, 2019 · 5 comments
Open

Comments

@nguyentranchung
Copy link

image

@dweirich
Copy link

dweirich commented Aug 28, 2019

Seems like the problem is an update on the @types/passport declaration: DefinitelyTyped/DefinitelyTyped@91c229d#comments

I fixed it by augmenting with a file in /src/types using the following

import { UserDocument } from '../models/User'

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

@chase-oneill
Copy link

Thanks for this.

@peterblazejewicz
Copy link
Collaborator

Let me know if that works:
Thanks!

import express from 'express';
import { UserDocument } from '../models/User';

declare module 'express' {
  export interface User extends UserDocument {}
  export interface Request {
    user?: User;
  }
}
``

@akoushke
Copy link

akoushke commented May 3, 2020

In case you get the Duplicate Key Error, make sure you use interface not type even if the linter complains. make sure to disable it:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as MongoUserSchema from '../../src/mongo/models/User';

declare global {
  namespace Express {
    // eslint-disable-next-line @typescript-eslint/no-empty-interface
    interface User extends MongoUserSchema.User {}
  }
}

TypeScript Rules!

@acerspyro
Copy link

acerspyro commented Jun 8, 2020

This is the solution that worked for me:

export {};

declare global {
  namespace Express {
    interface User {
      username: string;
      id?: string;
      profile?: any;
      // Add whatever you're missing
    }
  }
}

You can place this within a .d.ts file that is within scope of your project.
It'll stop TypeScript from complaining, but the usage of any here is iffy at best.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants