Is there a way to "extract" the type of TypeScript interface property?
Asked Answered
M

6

348

Let's suppose there's a typing file for library X which includes some interfaces.

interface I1 {
    x: any;
}
    
interface I2 {
    y: {
        a: I1,
        b: I1,
        c: I1
    }
    z: any
}

In order to work with this library I need pass around an object that is of exactly the same type as I2.y. I can of course create identical interface in my source files:

interface MyInterface {
    a: I1,
    b: I1,
    c: I1
}

let myVar: MyInterface;

but then I get the burden of keeping it up to date with the one from library, moreover it can be very large and result in lot of code duplication.

Therefore, is there any way to "extract" the type of this specific property of the interface? Something similar to let myVar: typeof I2.y (which doesn't work and results in "Cannot find name I2" error).


Edit: after playing a bit in TS Playground I noticed that following code achieves exactly what I want to:

declare var x: I2;
let y: typeof x.y;

However it requires a redundant variable x to be declared. I am looking for a way to achieve this without that declaration.

Meingoldas answered 30/3, 2016 at 14:6 Comment(2)
which doesn't work - how does that manifest? What's the actual error message that you see?Fielder
@BartekBanachewicz updatedMeingoldas
E
618

It wasn't possible before but luckily it is now, since TypeScript version 2.1. It was released on the 7th of December 2016 and introduces indexed access types, also called lookup types.

The syntax looks like element access but is written in place of types. So in your case:

interface I1 {
    x: any;
}

interface I2 {
    y: {
        a: I1,
        b: I1,
        c: I1
    }
    z: any
}

let myVar: I2['y'];  // indexed access type

Now myVar has the type of I2.y.

Check it out in TypeScript Playground.

Evy answered 10/12, 2016 at 13:5 Comment(8)
In the case when 'y' is an array, is there any way to extract the type of the elements? e.g. I2{ y: {..}[]}Ivon
@JohnB yes, you can do it in exactly same way, because array indices are just like object properties. Check it out here: typescriptlang.org/play/…Meingoldas
@JohnB yes, you can access it the same way ie. I2['y'][0] See: typescriptlang.org/play/…Undone
Let's assume we're looping over keys of an object defined using I2 as the type. How would I get the type of a specific key dynamically when looping. This; let z: typeof x[a];, where a is a certain key as a string, does not work. It tells me that a refers to a value and has to refer to a type. How would I go about doing this? Is it even possible in any way? Thx!Hyperphysical
It's impossible to say without any code. Post a playground link and I'll take a look.Undone
Also see the handbook on Indexed Access Types . (via)Authorize
Please note that at some time in the history of evolving TypeScript myVar has become undefined, though, wherefore you really can't do much with it. Look at the TypeScript Playground in the answer, which does not compile.Barrens
Fixed @TorbenKochPløen. The syntax didn't change, it's just that my example was in fact using the myVar without assigning anything to it.Undone
R
36

To expand on the accepted answer, you can also assign the type using the type keyword and use it in other places.

// Some obscure library
interface A {
  prop: {
    name: string;
    age: number;
  }
}

// Your helper type
type A_Prop = A['prop']

// Usage
const myThing: A_prop = { name: 'June', age: 29 };
Reimer answered 19/1, 2021 at 0:52 Comment(0)
W
23

Just an example of extracting a literal type from the union object type:

type Config = {
    key: "start_time",
    value: number,
} | {
    key: "currency",
    value: string,
}

export type ConfigKey = Config["key"];
// "start_time"|"currency"

Whitmire answered 26/4, 2022 at 20:19 Comment(1)
This is the answer I was looking for. I wounder why it's not upvoted. Anyway, thank you!Euroclydon
F
20

keyof Colors will return a list of all keys "white" | "black". When this list of keys gets passed to the Colors interface, the type will be all the values of the given keys, "#fff" | #000.

interface Colors {
  white: "#fff"
  black: "#000"
}

type ColorValues = Colors[keyof Colors]
// ColorValues = "#fff" | "#000"
Flicker answered 13/1, 2022 at 18:21 Comment(0)
N
-1
const foo = ()=>{
   return {name: "test", age: 5}
}
type T1 = ReturnType<typeof foo> // {name: string, age: number}
type T2 = ReturnType<typeof foo>['name'] // string
type T3 = T1['age'] // number
Noontide answered 16/11, 2023 at 2:51 Comment(0)
B
-5

An interface is like the definition of an object. Then y is a property of your I2 object, that is of a certain type, in that case "anonymous".

You could use another interface to define y and then use it as your y type like this

interface ytype {
   a: I1;
   b: I1;
   c: I1;
}

interface I2 {
    y: ytype;
    z: any;
}

You can put your interface in a file and use extract so you can import it in other files of your projects

export interface ytype {
   a: I1;
   b: I1;
   c: I1;
}



 export interface I2 {
        y: ytype;
        z: any;
    }

You can import it that way :

   import {I1, I2, ytype} from 'your_file'
Buoyancy answered 30/3, 2016 at 14:15 Comment(1)
Everything's fine, but as I mentioned - the interfaces I1 and I2 come from an external library and are defined in d.ts file for that library. Therefore having this ytype interface would be a code duplication and would need to be constantly updated.Meingoldas

© 2022 - 2024 — McMap. All rights reserved.