Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  193] [ 3]  / answers: 1 / hits: 15830  / 2 Years ago, mon, january 10, 2022, 12:00:00

I have an interface, which should have keys that are in specific enum key type, but when I declare the type it gives this error:



A mapped type may not declare properties or methods.



Here is the code:


enum myEnum {
propOne = 'propOne',
propTwo = 'propTwo'
}

export interface myInterface {
[key in myEnum]: anotherInterface;
}

I tried to specify the type also like this, but it didn't work and gave me syntax error:


export interface myInterface {
[key in keyof typeof myEnum]: anotherInterface;
}

I also tried to use a normal object instead of an enum, but it gave the same error.


More From » typescript

 Answers
23

You need to use Mapped type, not interface:


export type MyInterface = {
[key in myEnum]: AnotherInterface;
}


[#50089] Tuesday, November 23, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jack

Total Points: 557
Total Questions: 96
Total Answers: 80

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;