[英]Map<string, enum> is not assignable to type { [key: string]: enum }'. Index signature is missing in type 'Map<string, enum>'
鉴于:
export enum Relationship {}
export interface Invitee {}
export interface InviteesResponse {
invitees: Invitee[];
relationships: { [key: string]: Relationship }; // map userIds to relationships
}
async getInvitees(userId: string): Promise<InviteesResponse> {
...
const invitees: Invitee[] = [];
const relationships: Map<string, Relationship> = new Map();
...
// this line throws the error on relationships
return { invitees, relationships };
Webstorm 建议进行以下修复:
async getInvitees(userId: string): Promise<{ invitees: Invitee[]; relationships: Map<string, Relationship> }> {
我想我只是不明白核心问题。 为什么它强迫我解构类型只是为了返回?
这又是一个完整的错误:
Type 'Map<string, Relationship>' is not assignable to type '{ [key: string]: Relationship; }'. Index signature is missing in type 'Map<string, Relationship>'.
对我来说问题很简单。 relationships: { [key: string]: Relationship };
不是 map 的正确语法。
应该是: relationships: Map<string, Relationship>;
我不知道为什么我认为地图没有明确的 typescript 类型,但现在一切都很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.