简体   繁体   English

错误 TS2749:“IRequestWithUser”指的是一个值,但在此处用作类型。 您的意思是“typeof IRequestWithUser”吗?

[英]error TS2749: 'IRequestWithUser' refers to a value, but is being used as a type here. Did you mean 'typeof IRequestWithUser'?

I am extending Request in express library to contain a user property:我在express库中扩展Request以包含用户属性:

import { Request } from 'express';

export default interface RequestWithUser extends Request {
  user: {
    user_name: string;
    password: string;
  }
}

The title error appears in the first parameter annotation:标题错误出现在第一个参数注释中:

import IRequestWithUser from '../shared/interfaces/isRequestWithUser';

const router: Router = Router();

router.post('/myRoute', async (req: IRequestWithUser, res: Response) => {

/*
error TS2749: 'IRequestWithUser' refers to a value, 
but is being used as a type here. Did you mean 
'typeof IRequestWithUser'?
*/

I don't believe interfaces are values.我不相信接口是值。 They should purely be types.它们应该纯粹是类型。 So what is causing this error?那么是什么导致了这个错误呢?

Also tried也试过

typeof IRequestWithUser This results in No overload matches this call typeof IRequestWithUser这导致No overload matches this call

Perhaps there's something missing from your code snippets, but the only error I get on Typescript 3.9 is one that relates to the overload error that you allude to later in your question.也许您的代码片段中缺少某些内容,但我在 Typescript 3.9 上遇到的唯一错误是与您稍后在问题中提到的重载错误有关的错误。

Because the .post() call is looking for a callback that expects type Request , you can't type your req argument as IRequestWithUser .由于.post()调用正在寻找需要类型Request的回调,因此您不能将req参数键入为IRequestWithUser This is because, although IRequestWithUser will contain all properties of Request , the Typescript compiler can't guarantee that the extra properties you plan to have in req will be there when the callback is run.这是因为,尽管IRequestWithUser将包含Request所有属性,但 Typescript 编译器无法保证您计划在req拥有的额外属性在运行回调时会存在。

The recommended way to extend Express types is to use interface merging .扩展 Express 类型的推荐方法是使用接口合并 This allows you to "redefine" the Request type altogether, but it's a global definition so it gets messy:这允许你完全“重新定义” Request类型,但它是一个全局定义,所以它变得混乱:

// definitions/express/index.d.ts
import * as express from 'express' // Unfortunately, you need some kind of import here to make this a valid module.

declare module "express-serve-static-core" {
    export interface Request {
        user: {
            user_name: string;
            password: string;
        }
    }
}

Your compiler should pick this file up on its own and support your new property using the original Request type.您的编译器应自行选择此文件并使用原始Request类型支持您的新属性。 You might need to have an explicit typeRoots set in your tsconfig.json if you're using something like ts-node though.您可能需要有一个明确的typeRoots在您设置tsconfig.json如果你正在使用类似ts-node虽然。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'GraphQLJSONObject' 指的是一个值,但在这里被用作一种类型。 您的意思是“typeof GraphQLJSONObject”吗?ts(2749) - 'GraphQLJSONObject' refers to a value, but is being used as a type here. Did you mean 'typeof GraphQLJSONObject'?ts(2749) TS2749:'Schema' 指的是一个值,但在定义 Mongoose model 和 TypeScript 时在这里用作类型 - TS2749: 'Schema' refers to a value, but is being used as a type here when defining Mongoose model with TypeScript 错误TS2693:“地图”仅引用类型,但在此处被用作值 - error TS2693: 'Map' only refers to a type, but is being used as a value here TS:模块扩充, <Type> 仅引用类型,但在此处用作值 - TS: Module augmentation, <Type> only refers to a type but is being used as a value here 指的是一个值,但在这里被用作一个类型 - refers to a value, but is being used as a type here &#39;ObjectId&#39; 仅指一种类型,但在此处用作值。ts(2693) - 'ObjectId' only refers to a type, but is being used as a value here.ts(2693) TS2585:“Promise”仅指类型,但在此处用作值 - TS2585: 'Promise' only refers to a type, but is being used as a value here nodejs mongoose 错误 TS2551:“文档”类型上不存在属性“isDeleted”。 你的意思是'$isDeleted' - nodejs mongoose error TS2551: Property 'isDeleted' does not exist on type 'Document'. Did you mean '$isDeleted' 错误TS2507:类型&#39;typeof Tapable&#39;不是构造函数类型 - error TS2507: Type 'typeof Tapable' is not a constructor function type TypeScript NodeJS 应用程序错误:元素隐式具有“任何”类型,因为类型“typeof globalThis”没有索引签名.ts(7017) - TypeScript NodeJS application Error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM