I want to create type for second object (anotherConfigurations
) to have same keys as first object (configurations
). And I would like the first object to be the source of keys
type SomeConfiguration = {
type: 'something' | 'somethingelse'
prop: string;
}
type AnotherConfiguration = {
color: string;
}
const configurations: Record<string, SomeConfiguration> = {
foo: {
type: 'something',
prop: 'kaa'
},
bar: {
type: 'somethingelse',
prop: 'boom'
}
}
const anotherConfigurations: Record<keyof typeof configurations, AnotherConfiguration> = {
foo: {
color: 'blue'
},
bar: {
color: 'red'
}
}
// //////////////////
type KeysOfConfiguration = keyof typeof configurations; // equal to type KeysOfConfiguration = string
I can remove Record
definition from configurations
and with this
keyof typeof configurations
will be equal to 'foo' | 'bar'
.
But with this solution I’m loosing restriction for property values for configurations
object
Another solution i came with is to create separate union type that contains all keys. But i want to keep configurations
as source of those keys
I want to kill two birds with one stone. configurations
must have restricted properties values and must be the source for the keys
Playground
nouc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.