Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Reference subtype of type definition (interface)

I am using the following type in my TypScript:

interface ExerciseData {     id : number;     name : string;     vocabulary : {         from : string;         to : string;     }[]; } 

Now I'd like to create a variable that is of the same type as the attribute vocabulary, trying the following:

var vocabs : ExerciseData.vocabulary[]; 

But that is not working. Is it possible to reference to a subtype somehow? Or would I have to do something like this?

interface ExerciseData {     id : number;     name : string;     vocabulary : Vocabulary[]; }  interface Vocabulary {         from : string;         to : string; }  var vocabs : Vocabulary[]; 

Thanks a lot for hints.

like image 465
Mathias Bader Avatar asked Jan 10 '15 11:01

Mathias Bader


People also ask

What is type {} TypeScript?

Summary. The TypeScript object type represents any value that is not a primitive value. The Object type, however, describes functionality that available on all objects. The empty type {} refers to an object that has no property on its own.

What is the difference between type and interface in TypeScript?

The typescript type supports only the data types and not the use of an object. The typescript interface supports the use of the object. Type keyword when used for declaring two different types where the variable names declared are the same then the typescript compiler will throw an error.

How do I inherit an interface in TypeScript?

Interfaces and Inheritance An interface can be extended by other interfaces. In other words, an interface can inherit from other interface. Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.

Does TypeScript enforce types?

Bookmark this question.


2 Answers

You can reference interface subtypes using lookup types, added in TypeScript 2.1:

interface ExerciseData {     id: number;     name: string;     vocabulary: Array<{         from: string;         to: string;     }>; }  type Name = ExerciseData['name']; // string 

These lookup types can also be chained. So to get the type of a vocabulary item you can do this:

type Vocabulary = ExerciseData['vocabulary'][number]; // { from: string; to: string; } 

Or with even more chaining, the from field:

type From = ExerciseData['vocabulary'][number]['from']; // string 

For complex scenarios it's also possible to base the lookup type on another type. For example, on a string literal union type:

type Key = 'id' | 'name'; type FieldTypes = ExerciseData[Key]; // number | string 
like image 128
Duncan Luk Avatar answered Sep 19 '22 01:09

Duncan Luk


I found it's working in the next way these days:

interface User {   avatar: string; }  interface UserData {   someAvatar: User['avatar']; } 

very useful if you don't want to export all the things.

like image 44
Vladimir Tolstikov Avatar answered Sep 22 '22 01:09

Vladimir Tolstikov