Say I have an interface such as:
interface MyInterface {
myProperty: {
one: number
two: string
}
}
How can I Pick myProperty fields? Is this possible?
The desired result should be:
{
one: number
two: string
}
So that when using the type:
type MyType = ...
const t: MyType = ...
t.one = ...
You just need to use a type query if you want to get the type of the member :
interface MyInterface {
myProperty: {
one: number
two: string
}
}
type MyType = MyInterface['myProperty']
const t: MyType = {
one: 1,
two: '2'
};
t.one = 3
Although refactoring to a separate type as suggested by another answer may be the saner way to go if possible.
You can create a separate interface for myProperty:
interface MyProperty {
one: number
two: string
}
interface MyInterface {
myProperty: MyProperty
}
const myObject: MyProperty = { one: 1, two: "2" };
And then use it throughout your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With