Types do not exist in emitted code - you can't go from a type to an array.
But you could go the other way around, in some situations. If the array is not dynamic (or its values can be completely determined by the type-checker at the time it's initialized), you can declare the array as const (so that the array's type is ["aaa", "bbb", "ccc"] rather than string[]), and then create a type from it by mapping its values from arr[number]:
const arr = ["aaa", "bbb", "ccc"] as const;
type myCustomType = typeof arr[number];
Here's an example on the playground.
Answer from CertainPerformance on Stack OverflowHow to convert typescript types of strings to array of strings?
TypeScript array to string literal type - Stack Overflow
How to define an array of strings in TypeScript interface? - Stack Overflow
What is {[key: string]: string} and how do I turn it into an array?
Videos
Types do not exist in emitted code - you can't go from a type to an array.
But you could go the other way around, in some situations. If the array is not dynamic (or its values can be completely determined by the type-checker at the time it's initialized), you can declare the array as const (so that the array's type is ["aaa", "bbb", "ccc"] rather than string[]), and then create a type from it by mapping its values from arr[number]:
const arr = ["aaa", "bbb", "ccc"] as const;
type myCustomType = typeof arr[number];
Here's an example on the playground.
For example:
const themeMode = ['light', 'dark'] as const;
type MainTheme = typeof themeMode[number];
const values = [...themeMode];
// iterate on themeMode as const assertion
values.map((theme) => console.log(theme));
TypeScript 3.4+
TypeScript version 3.4 has introduced so-called **const contexts**, which is a way to declare a tuple type as immutable and get the narrow literal type directly (without the need to call a function like shown below in the 3.0 solution).With this new syntax, we get this nice concise solution:
const furniture = ['chair', 'table', 'lamp'] as const;
type Furniture = typeof furniture[number];
More about the new const contexts is found in this PR as well as in the release notes.
TypeScript 3.0+
With the use of generic rest parameters, there is a way to correctly infer string[] as a literal tuple type and then get the union type of the literals.
It goes like this:
const tuple = <T extends string[]>(...args: T) => args;
const furniture = tuple('chair', 'table', 'lamp');
type Furniture = typeof furniture[number];
More about generic rest parameters
This answer is out of date; see @ggradnig's answer.
The best available workaround:
const furnitureObj = { chair: 1, table: 1, lamp: 1 };
type Furniture = keyof typeof furnitureObj;
const furniture = Object.keys(furnitureObj) as Furniture[];
Ideally we could do this:
const furniture = ['chair', 'table', 'lamp'];
type Furniture = typeof furniture[number];
Unfortunately, today furniture is inferred as string[], which means Furniture is now also a string.
We can enforce the typing as a literal with a manual annotation, but it brings back the duplication:
const furniture = ["chair", "table", "lamp"] as ["chair", "table", "lamp"];
type Furniture = typeof furniture[number];
TypeScript issue #10195 tracks the ability to hint to TypeScript that the list should be inferred as a static tuple and not string[], so maybe in the future this will be possible.
interface Addressable {
address: string[];
}
An array is a special type of data type which can store multiple values of different data types sequentially using a special syntax.
TypeScript supports arrays, similar to JavaScript. There are two ways to declare an array:
- Using square brackets. This method is similar to how you would declare arrays in JavaScript.
let fruits: string[] = ['Apple', 'Orange', 'Banana'];
- Using a generic array type, Array.
let fruits: Array<string> = ['Apple', 'Orange', 'Banana'];
Both methods produce the same output.
Of course, you can always initialize an array like shown below, but you will not get the advantage of TypeScript's type system.
let arr = [1, 3, 'Apple', 'Orange', 'Banana', true, false];
Source
The order of the value-strings doesn't matter, I just want them in an array.
Also, what is this called? I couldn't find anything about this, partly because I don't know its name.
Building on top of jcal's answer and reiterating: "You can't convert types in TypeScript to values at runtime. But you can do the reverse: create a runtime object and have TypeScript infer its type." There is a great blog post by @steve-holgado to use const assertions for this purpose: https://steveholgado.com/typescript-types-from-arrays/
With typescript's const assertions (TypeScript 3.4+) you can do:
const animals = ['cat', 'dog', 'mouse'] as const
type Animal = typeof animals[number]
// type Animal = 'cat' | 'dog' | 'mouse'
So the question's code would look like:
const imageVerticalSpacing = ['ignoreBottom', 'ignoreTop', 'ignoreBoth', 'Default'];
type ImageVerticalSpacing = typeof imageVerticalSpacing[number];
You can't convert types in TypeScript to values at runtime. But you can do the reverse: create a runtime object and have TypeScript infer its type.
The ideal runtime object for this purpose would be a tuple. Unfortunately, TypeScript doesn't infer tuples that well by itself. I use a helper function called tuple() which returns tuple types.
UPDATE: 2018-12, since TypeScript 3.0 the tuple() function can be written like this:
type Narrowable = string | number | boolean | symbol |
object | {} | void | null | undefined;
const tuple = <T extends Narrowable[]>(...args: T)=>args;
Using the above helper function, you can do this:
const imageVerticalSpacing = tuple('ignoreBottom','ignoreTop','ignoreBoth','Default');
type ImageVerticalSpacing = (typeof imageVerticalSpacing)[number];
The imageVerticalSpacing object is an array of strings you can use for your dropdown, of type ['ignoreBottom','ignoreTop','ignoreBoth','Default']. And the type ImageVerticalSpacing is the same 'ignoreBottom' | 'ignoreTop' | 'ignoreBoth' | 'Default' as you declared.
(See it in action on The Playground)
Hope that helps. Good luck!