声明合并不适用于快速 4.17.* 请求类型



我想向请求类型添加一个属性,所以我创建了一个文件夹@types/express,并在该文件夹中添加了包含此内容的文件index.d.ts。

namespace Express {
interface Request {
user: number;
}
}

在VSCode中,当我引用req.user时,错误已经消失,它甚至表明usernumber类型屏幕截图显示;用户";";请求";对象被视为正确的

但当我启动服务器时,我看到错误,上面写着:

/home/myself/web/my-server/node_modules/ts-node/src/index.ts:434
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/app.ts:46:7 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
46   req.user;
~~~~
at createTSError (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:434:12)
at reportTSError (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:438:19)
at getOutput (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:578:36)
at Object.compile (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:775:32)
at Module.m._compile (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:858:43)
at Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Object.require.extensions.<computed> [as .ts] (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:861:12)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

如果有任何关于如何修复它的想法,我将不胜感激。

p.s.我对express-session模块做了同样的操作,并在Session接口中添加了counter属性,它完美地工作在中

安装了@types/express(@types/express-session也足够了(,这应该可以工作:

index.d.ts

declare module '@types/express-serve-static-core' {
interface Request {
user?: User
}
}

重要的一点是,它的声明合并逻辑是@types/express-serve-static-core的一部分(请参见https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-serve-static-core/index.d.ts)。

在失去一些脑细胞后,我给自己提供了答案。为了解决这个问题,我必须重新创建可以在这个问题中找到的文件夹结构https://github.com/microsoft/TypeScript/issues/39581

然后我修改了tsconfig.json,使其类型Roots看起来像这个

"typeRoots": [
"src/typings/",
"node_modules/@types/"
] 

然后,为了用我的自定义类型来增强请求类型,我必须使用import表达式,所以最后index.d.ts文件在中会有这个

declare namespace Express {
export interface Request {
user: import("mongoose").Model<import("./../../models/user").User>;
}
}

评论中的家伙的tsconfig

{
"compilerOptions": {
/* Enable incremental compilation */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": [
"es6"
] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
/* Concatenate and emit output to single file. */
"outDir": "build" /* Redirect output structure to the directory. */,
"rootDir": "src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
/* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": [
"src/typings/",
"node_modules/@types/"
] /* List of folders to include type definitions from. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
/* Advanced Options */
"resolveJsonModule": true /* Include modules imported with '.json' extension */,
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

最新更新