[英]Map<string, enum> is not assignable to type { [key: string]: enum }'. Index signature is missing in type 'Map<string, enum>'
Given:鉴于:
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 suggests the following fix: Webstorm 建议进行以下修复:
async getInvitees(userId: string): Promise<{ invitees: Invitee[]; relationships: Map<string, Relationship> }> {
I guess I just don't understand the core issue.我想我只是不明白核心问题。 Why is it forcing me to destructure the type just to return?为什么它强迫我解构类型只是为了返回?
And here's the full-ish error again:这又是一个完整的错误:
Type 'Map<string, Relationship>' is not assignable to type '{ [key: string]: Relationship; }'. Index signature is missing in type 'Map<string, Relationship>'.
The issue for me was simple.对我来说问题很简单。 relationships: { [key: string]: Relationship };
is not the correct syntax for a map.不是 map 的正确语法。
It should be: relationships: Map<string, Relationship>;
应该是: relationships: Map<string, Relationship>;
I don't know why I thought that maps didn't have a an explicit typescript type but they do so everything is good now!我不知道为什么我认为地图没有明确的 typescript 类型,但现在一切都很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.