0

I have an object defined as:

const dict = {
    "A": "a",
    "B": "b",
    "C": "c"
};

and a type defined as type Capital = "A" | "B" | "C";.
The type that typescript has automatically assign to dict is

const dict: {
    A: string,
    B: string,
    C: string,
}

Now my question is, is there a way to declare dict in a way to use Capital?
Something like const dict: {Capital: string}?

1 Answer 1

2

You are looking for Record<Keys, Type> type. More info.

Solution for your case:

type Capital = "A" | "B" | "C";
const dict: Record<Capital, string> = {
    "A": "a",
    "B": "b",
    "C": "c"
};

(playground link)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.