简体   繁体   中英

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:

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.

It should be: 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM