Zagaek98y
Zagaek98y

Reputation: 99

How to declare a new object type in typescript

When the type is declared as in the code block below

When declaring a new type C is an object, the key value is the key value of A, and the type of each key is B.

How to declare C with only A and B..?

type A = {
 home: string
 info:string
 about:string
}
type B = "open" | "close"

desired result

type C = {
 home: "open" | "close"
 info:"open" | "close"
 about:"open" | "close"
}

Upvotes: 1

Views: 193

Answers (2)

kaya3
kaya3

Reputation: 51152

Using the Record type:

type C = Record<keyof A, B>

Upvotes: 1

MrCodingB
MrCodingB

Reputation: 2314

You could use in like this:

type C = { [K in keyof A]: B };

Upvotes: 3

Related Questions