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 Overflow
🌐
TypeScript Tutorial
typescripttutorial.net › home › typescript tutorial › typescript array type
TypeScript Array Type
August 24, 2024 - TypeScript arrays have the same properties and methods as JavaScript arrays. For example, the following uses the length property to get the number of elements in an array:
Discussions

TypeScript array to string literal type - Stack Overflow
I currently have both an array of strings and a string literal union type containing the same strings: const furniture = ['chair', 'table', 'lamp']; type Furniture = 'chair' | 'table' | 'lamp'; I ... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to create a typescript type from an array? - Stack Overflow
I often use code such as export type Stuff = 'something' | 'else' export const AVAILABLE_STUFF: Stuff[] = ['something', 'else'] This way I can both use the type Stuff, and iterate over all the More on stackoverflow.com
🌐 stackoverflow.com
Creating types from values in array
TypeScript Version: 3.0.3 Search Terms: Type based on values in array Is there a current or planned feature to create a type from strings in an array? Code const values = ['A', 'B'] type Foo = OneO... More on github.com
🌐 github.com
28
October 22, 2018
Array type syntax preference
Both. T[] for when the T is a simple identifier. Array where T is a more complex inline type. More on reddit.com
🌐 r/typescript
20
10
June 9, 2022
🌐
W3Schools
w3schools.com › typescript › typescript_arrays.php
TypeScript Arrays
const names: string[] = []; names.push("Dylan"); // no error // names.push(3); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. Try it Yourself » · The readonly keyword can prevent arrays from being changed. const names: readonly string[] = ["Dylan"]; names.push("Jack"); // Error: Property 'push' does not exist on type 'readonly string[]'. // try removing the readonly modifier and see if it works? Try it Yourself » · TypeScript can infer the type of an array if it has values.
Top answer
1 of 4
383

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

2 of 4
22

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.

🌐
TkDodo
tkdodo.eu › blog › array-types-in-type-script
Array Types in TypeScript
August 19, 2023 - To be clear, there is absolutely no functional difference between the two notations. It seems that its just personal preference about which one to choose. Whatever you do, please make sure to turn on the array - type (opens in a new window) eslint rule to use either one of the notations consistently.
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-array
TypeScript Arrays
This method is similar to how you would declare arrays in JavaScript. let fruits: string[] = ['Apple', 'Orange', 'Banana']; 2. Using a generic array type, Array<elementType>. 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.
🌐
Ultimate Courses
ultimatecourses.com › blog › typing-arrays-typescript
Typing Arrays in TypeScript - Ultimate Courses
January 12, 2023 - There are many ways in TypeScript to type a property as an array, or contains an array of “something”. These ways of declaring an array...
Find elsewhere
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
To specify the type of an array like [1, 2, 3], you can use the syntax number[]; this syntax works for any type (e.g. string[] is an array of strings, and so on). You may also see this written as Array<number>, which means the same thing. We’ll learn more about the syntax T<U> when we cover ...
Top answer
1 of 3
48

With TypeScript v3.4 const assertions:

export const AVAILABLE_STUFF = <const> ['something', 'else'];
export type Stuff = typeof AVAILABLE_STUFF[number];
2 of 3
17

One built-in option would be to use an enum instead of the type and array approach.

export enum Stuff {
    something = 'something',
    else = 'else',
}

export const AVAILABLE_STUFF: Stuff[] = Object.values(Stuff);

Another option is to extract the type from the type of AVAILABLE_STUFF. To do this we must force the compiler to infer a tuple of string literals for AVAILABLE_STUFF. This can be done in 3.4 with as const or before 3.4 using an extra function. After AVAILABLE_STUFF is a tuple type we can just use a type query to get the type of the elements:

export const AVAILABLE_STUFF = (<T extends string[]>(...o: T)=> o)('something', 'else'); // typed as ["something", "else"]
// export const AVAILABLE_STUFF = ['something', 'else'] as const; // typed as ["something", "else"]  in 3.4
export type Stuff = typeof AVAILABLE_STUFF[number] //"something" | "else"

A few explanations of the above code. typeof AVAILABLE_STUFF gives us the type of the constant (["something", "else"]) to get the [number] is called a type query and will give us the type of an item in the tuple.

The (<T extends string[]>(...o: T)=> o) is just an IIFE we use to force the compiler to infer a string literal tuple type. It has to be generic as the compiler will only infer literal types and tuples in certain cases (a type parameter with a constraint of string being one of them). The as const version is what I would recommend using when it becomes available as it is more readable.

🌐
Index.dev
index.dev › blog › typescript-cast-to-array-guide
How to Cast to an Array in TypeScript: A Quick Guide
December 30, 2024 - Learn how to cast data to arrays in TypeScript using simple methods. This guide provides step-by-step instructions and code examples to help you get started.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 28046
Creating types from values in array · Issue #28046 · microsoft/TypeScript
October 22, 2018 - TypeScript Version: 3.0.3 Search Terms: Type based on values in array Is there a current or planned feature to create a type from strings in an array? Code const values = ['A', 'B'] type Foo = OneO...
Author   G2Jose
🌐
Medium
jng15.medium.com › typescript-basics-iii-arrays-36750905ee15
TypeScript Basics (III): Arrays. Type annotation for arrays can be… | by James Ng | Medium
November 16, 2022 - Time for the most common data structure to get “typed” up — arrays. Type annotation for arrays can be assigned by simply putting a set of square brackets “[ ]” after the element type.
🌐
TutorialsPoint
tutorialspoint.com › typescript › typescript_arrays.htm
TypeScript - Arrays
The type of such an array is inferred from the data type of the arrays first element during initialization. For example, a declaration like − var numlist:number[] = [2,4,6,8] will create an array as given below − · The array pointer refers ...
🌐
2ality
2ality.com › 2020 › 02 › typing-arrays-typescript.html
Typing Arrays in TypeScript
February 24, 2020 - Both array type literals and Array require all elements to have the same type.
🌐
Educative
educative.io › home › courses › using typescript with react › creating a strongly-typed array
Creating Strongly Typed Arrays in TypeScript for React
There’s an alternative and arguably simpler method of creating strongly-typed arrays which is to put the type of the array items followed by squared brackets: ... Use the square bracket notation to create a variable called numbers that is assigned to an array of strings containing "one", "two", and "three" in the code widget below. ... So, TypeScript can cleverly infer the type of an array.
🌐
Total TypeScript
totaltypescript.com › array-types-in-typescript
Array<T> vs T[]: Which is better? | Total TypeScript
January 14, 2025 - When declaring array types in TypeScript, you can choose between `Array ` and `T[]`. Both are identical, but there are some considerations to keep in mind.
🌐
TypeScript ESlint
typescript-eslint.io › rules › array-type
array-type | typescript-eslint
TypeScript provides two equivalent ways to define an array type: T[] and Array<T>. The two styles are functionally equivalent.
🌐
HackerNoon
hackernoon.com › what-is-an-array-type-in-typescript
What Is an Array Type in Typescript | HackerNoon
July 12, 2022 - Arrays work the same in TypeScript as they do in Javascript, the only difference being that we have to define their type upfront.