If I have an interface in typescript like so
JavaScript
x
6
1
interface MyInterface {
2
id: number;
3
image: string;
4
text: "one thing" | "another" | "anotherthing" | "anotherthing"
5
}
6
How can I factor out my type for text with the pipes so I can use it multiple times as a type in my program? It seems like it should be simple but I’m not sure what the syntax is
For clarity I would like to be able to use it like
JavaScript
1
13
13
1
factoredOutType = "one thing" | "another" | "anotherthing" | "anotherthing"
2
3
interface MyInterface1 {
4
id: number;
5
image: string;
6
text: factoredOutType
7
}
8
9
interface MyInterface2 {
10
name: factoredOutType
11
}
12
13
etc… I can’t seem to find the syntax to this question so help is appreciated.
Advertisement
Answer
I think you just need the type
keyword:
JavaScript
1
2
1
type factoredOutType = "one thing" | "another" | "anotherthing" | "anotherthing"
2
Not near a compiler now so can’t really check but I think that works