Imagine a scenario where there is a typing file for library X that contains some interfaces.
interface I1 {
x: any;
}
interface I2 {
y: {
a: I1,
b: I1,
c: I1
}
z: any
}
When working with this library, it becomes necessary to pass around an object of exactly the same type as I2.y
. One option is to create an identical interface in the source files:
interface MyInterface {
a: I1,
b: I1,
c: I1
}
let myVar: MyInterface;
However, this approach introduces the burden of keeping it in sync with the library's interface, leading to potential code duplication and maintenance overhead.
Is there a way to "extract" the type of this specific property from the interface? Perhaps something like let myVar: typeof I2.y
(which currently results in a "Cannot find name I2" error).
Edit: After experimenting in TS Playground, it was discovered that the following code achieves the desired outcome:
declare var x: I2;
let y: typeof x.y;
However, this method requires declaring a redundant variable x
. The goal is to find a solution without the need for this declaration.