【问题标题】:Typescript declaration file not getting compiled properly打字稿声明文件未正确编译
【发布时间】:2022-11-15 16:54:24
【问题描述】:

我有这个lib.d.ts文件

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

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

我有这个护照.ts在同一个文件夹中,其中用户是类型快递.用户

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

IDE 没有抱怨它,但是当我运行应用程序时出现此错误:

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

这是我的 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,                         
  }
}

这是从 tsconfig json 到文件的路径:

  • src/config/passport/passport.ts
  • src/config/passport/lib.d.ts

为什么会发生这种情况,我已经尝试修复此错误 2 天了。

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    问题可能是因为您试图在 lib.d.ts 文件的顶部导入。我有一个类似的问题,在文件的第一行开始声明解决了我的问题。尝试这个。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-12
      • 2017-02-16
      • 2018-03-11
      • 2017-05-09
      • 1970-01-01
      • 2013-08-20
      • 2016-11-19
      • 1970-01-01
      相关资源
      最近更新 更多