Reputation: 12032
I'm trying to use mapped type on an enum:
export enum CurrencyType {
USD = "USD",
AUD = "AUD",
GBP = "GBP",
CAD = "CAD"
}
to achieve this type:
type Rates = {
AUD: number;
CAD: number;
GBP: number;
USD: number;
}
I thought I could do something like this:
type Rates = {
[P in keyof CurrencyType]: number;
};
but that results in this instead:
type Rates = {
toString: number;
charAt: number;
charCodeAt: number;
concat: number;
indexOf: number;
lastIndexOf: number;
localeCompare: number;
match: number;
replace: number;
//...
}
I tried using union literals
instead of enum
,
export type CurrencyType = "USD" | "AUD" | "GBP" | "CAD";`
but it gives the same result.
The only thing I can get to work, is by doing this:
type Rates = {
[P in typeof CurrencyType.AUD | CurrencyType.USD | CurrencyType.GBP | CurrencyType.CAD]: number;
};
but obviously that defeats the whole purpose!
Is there anyway to do this?
Upvotes: 4
Views: 2451
Reputation: 12032
wow it was right in front of me. As always, I was over complicating it and not reading the documentation thoroughly.
type Rate = {
[P in CurrencyType]: number;
};
I'll leave this up in case it helps someone else
Upvotes: 15