Javascript 类型';T';不可分配给类型';字典<;字符串>;
我正在尝试为get路由中的请求编写接口。你能帮忙吗。 该接口应扩展默认请求接口,但必须包含my id param 请参见下面的错误:Javascript 类型';T';不可分配给类型';字典<;字符串>;,javascript,node.js,typescript,Javascript,Node.js,Typescript,我正在尝试为get路由中的请求编写接口。你能帮忙吗。 该接口应扩展默认请求接口,但必须包含my id param 请参见下面的错误: { “所有者”:“类型脚本”, “代码”:“2430”, “严重性”:8, “消息”:“接口'IGetCustomRequest'不正确地扩展了接口'Request'。\n属性'params'的类型不兼容。\n类型'T'不可分配给类型'Dictionary'。”, “来源”:“ts”, “惊人数字”:17, “startColumn”:11, “endLineN
{
“所有者”:“类型脚本”,
“代码”:“2430”,
“严重性”:8,
“消息”:“接口'IGetCustomRequest'不正确地扩展了接口'Request'。\n属性'params'的类型不兼容。\n类型'T'不可分配给类型'Dictionary'。”,
“来源”:“ts”,
“惊人数字”:17,
“startColumn”:11,
“endLineNumber”:17,
“结束列”:28
}
类型中的属性仅限于具有字符串键和值的字典类型。您的IGetCustomRequest
中的T
未受约束,因此编译器抱怨请求的扩展不正确
查看express类型:
// copied from @types/express-serve-static-core
export interface Dictionary<T> {
[key: string]: T;
}
export type ParamsDictionary = Dictionary<string>;
export type ParamsArray = string[];
export type Params = ParamsDictionary | ParamsArray;
type Request<P extends Params = ParamsDictionary> = {
params: P;
};
interface IGetCustomRequest<T extends ParamsDictionary> extends Request {
params: T;
}