I have the following in a typing file:
interface A {
anObject: {
data: string;
};
someOtherData: boolean;
}
Now I want to change the interface so that anObject also includes data2. I want the final form to be
interface A {
anObject: {
data: string;
data2: string;
};
someOtherData: boolean;
}
I already tried doing this and it failed:
interface A {
anObject: {
data2: string;
}
}
Instead of the anObject having both data and data2, it only has data2. Is there anyway to keep original keys?
Hmm, how about something like this:
export interface A {
anObject: {
data: string;
};
someOtherData: boolean;
}
import {A as _A} from './orig'
interface A extends _A {
anObject: _A['anObject'] & {
data2: string;
}
}
That is: rename the original A out of the way to _A during import. Then extend it and merge in the new data2 property with the original by intersecting it with the looked-up anObject property.
Or, if you don't mind A being a type alias and not an interface, there's the simpler:
import {A as _A} from './playground'
type A = _A & {
anObject: {
data2: string;
}
}
... in which you still rename the original out of the way, but then simply intersect it with the new part. Either method gives you the type you want:
declare const a: A;
a.anObject.data.charAt(0); // okay
a.anObject.data2.charAt(0); // okay
Does that help?
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