You are better off using a native array instead of an object literal with number-like properties, so that numbering (as well as numerous other array functions) are taken care of off-the-shelf.

What you are looking for here is an inline interface definition for your array that defines every element in that array, whether initially present or introduced later:

let userTestStatus: { id: number, name: string }[] = [
    { "id": 0, "name": "Available" },
    { "id": 1, "name": "Ready" },
    { "id": 2, "name": "Started" }
];

userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]

If you are initializing your array with values right away, the explicit type definition is not a necessity; TypeScript can automatically infer most element types from the initial assignment:

let userTestStatus = [
    { "id": 0, "name": "Available" },
    ...
];

userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]
Answer from John Weisz on Stack Overflow
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › objects.html
TypeScript: Documentation - Object Types
Generic object types are often some sort of container type that work independently of the type of elements they contain. It’s ideal for data structures to work this way so that they’re re-usable across different data types. It turns out we’ve been working with a type just like that throughout this handbook: the Array type.
🌐
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 ...
Discussions

In Typescript how do you get an element type from an array type, when the array contains different element types? - Stack Overflow
It was very trivial in the end ... that typeof input[0] would return string, as that is the type of the first element, but obviously it wouldn't make sense at a type-level. Thanks! 2018-02-09T10:40:08.93Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Domain expertise still wanted: the latest trends in AI-assisted knowledge for... ... I’m Jody, the Chief Product and Technology Officer at Stack ... More on stackoverflow.com
🌐 stackoverflow.com
typescript - How to get type of array items? - Stack Overflow
If I have a type type foo = Array , would it be possible to get the type of the values within the array, in this case, the interface. I know there is keyof to ... More on stackoverflow.com
🌐 stackoverflow.com
Deriving a type from fields of objects in an array
Don't type foo as a string but as string literals? Or maybe don't type a, b and c like that but use satisfies? More on reddit.com
🌐 r/typescript
5
2
August 19, 2023
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
Top answer
1 of 11
643

You are better off using a native array instead of an object literal with number-like properties, so that numbering (as well as numerous other array functions) are taken care of off-the-shelf.

What you are looking for here is an inline interface definition for your array that defines every element in that array, whether initially present or introduced later:

let userTestStatus: { id: number, name: string }[] = [
    { "id": 0, "name": "Available" },
    { "id": 1, "name": "Ready" },
    { "id": 2, "name": "Started" }
];

userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]

If you are initializing your array with values right away, the explicit type definition is not a necessity; TypeScript can automatically infer most element types from the initial assignment:

let userTestStatus = [
    { "id": 0, "name": "Available" },
    ...
];

userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]
2 of 11
159

What you have above is an object, not an array.

To make an array use [ & ] to surround your objects.

userTestStatus = [
  { "id": 0, "name": "Available" },
  { "id": 1, "name": "Ready" },
  { "id": 2, "name": "Started" }
];

Aside from that TypeScript is a superset of JavaScript so whatever is valid JavaScript will be valid TypeScript so no other changes are needed.

Feedback clarification from OP... in need of a definition for the model posted

You can use the types defined here to represent your object model:

type MyType = {
    id: number;
    name: string;
}

type MyGroupType = {
    [key:string]: MyType;
}

var obj: MyGroupType = {
    "0": { "id": 0, "name": "Available" },
    "1": { "id": 1, "name": "Ready" },
    "2": { "id": 2, "name": "Started" }
};
// or if you make it an array
var arr: MyType[] = [
    { "id": 0, "name": "Available" },
    { "id": 1, "name": "Ready" },
    { "id": 2, "name": "Started" }
];
🌐
Graphite
graphite.com › guides › typescript-typeof-operator
TypeScript typeof operator - Graphite
In this example, typeof is used to fetch the type of the variables myNumber and myString, which are then used to define types NumberType and StringType, respectively. typeof can be applied to more complex data structures, such as arrays and objects, to extract their types.
🌐
Melvin George
melvingeorge.me › blog › get-object-type-from-array-of-objects-typescript
How to get the object type from an array of objects in TypeScript? | MELVIN GEORGE
January 24, 2022 - To get the object type from an array of objects first, we can use the typeof type operator followed by the array variable name or array literal followed by writing a square brackets symbol ([]) and inside the square brackets, we can write the ...
🌐
TypeScript
typescriptlang.org › play › javascript › javascript-essentials › objects-and-arrays.ts.html
TypeScript: Playground Example - Objects and Arrays
There's four new things here: type PurchaseOrder - Declares a new type to TypeScript. typeof - Use the type inference system to set the type based on the const which is passed in next. purchaseOrder - Get the variable purchaseOrder and tell TypeScript this is the shape of all objects in the ...
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-array
TypeScript Arrays
An array in TypeScript can contain elements of different data types using a generic array type syntax, as shown below. ... let values: (string | number)[] = ['Apple', 2, 'Orange', 3, 4, 'Banana']; // or let values: Array<string | number> = ['Apple', ...
Find elsewhere
🌐
Tim Mousk
timmousk.com › blog › typescript-array-of-objects
How To Define An Array Of Objects In TypeScript? – Tim Mouskhelichvili
March 6, 2023 - In TypeScript, to define an array of objects, you can: Use an existing type or interface. Use an inline type. Use the built-in Array type. Use the typeof operator.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-array-element-type
Get the Element type from an Array type in TypeScript | bobbyhadz
We used the infer keyword to have TypeScript fill in the type of the array element. Here is an oversimplified version of how the conditional type in the original example works. ... And here is the original code sample. ... Copied!type ArrElement<ArrType> = ArrType extends readonly (infer ElementType)[] ? ElementType : never; const arr1 = ['a', 'b']; // 👇️ type T1 = string type T1 = ArrElement<typeof arr1>;
🌐
Reddit
reddit.com › r/typescript › deriving a type from fields of objects in an array
r/typescript on Reddit: Deriving a type from fields of objects in an array
August 19, 2023 -

I want to create a type restricted to the values used by the objects in an array for a specific type.

I have it working, until I define a type for those objects in the array - then my type becomes just the type of the field in that type.

E.g.

const x = { foo: "foofy" } as const;
const y = { foo: "barfy" } as const;
const z = { foo: "bazfy" } as const;

const xyz = [x, y, z];

type XYZ = typeof xyz[number]["foo"];
// XYZ = "foofy" | "barfy" | "bazfy"

But adding a type:

type Fooey = { foo: string };

const a: Fooey = { foo: "foozy" } as const;
const b: Fooey = { foo: "barzy" } as const;
const c: Fooey = { foo: "bazzy" } as const;

const abc = [a, b, c];

type ABC = typeof abc[number]["foo"];
// ABC = string

Is there a way I can have my cake and eat it, here?

typescript playground

TIA!

🌐
Filosophy
filosophy.org › code › deriving-object-types-based-on-arrays-in-typescript
Deriving object types based on arrays in Typescript | Filosophy
Below I describe a useful technique that takes advantage of the benefits of both structures: deriving an object’s type from a single source of truth array describing the object, usable both as a value and a type: const catInfoFields = [ { key: 'name', type: 'string' }, { key: 'age', type: 'number' }, { key: 'isKitten', type: 'boolean', optional: true }, ] as const; // ... <described below> ... type CatInfo = FieldsObject<typeof catInfoFields>; const olivia: CatInfo = { name: 'Olivia', age: 6 }; const mozart: CatInfo = { name: 'Mozart', age: 1, isKitten: true };
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-can-i-define-an-array-of-objects-in-typescript
How can I Define an Array of Objects in TypeScript? - GeeksforGeeks
May 20, 2024 - In this method, we will first create an JavaScript object with some key value pairs inside it and then explicitly assign the type of that object to the array using the typeof operator and built-in array type.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 28046
Creating types from values in array · Issue #28046 · microsoft/TypeScript
October 22, 2018 - const values = ['A', 'B'] type Foo = OneOf<values> // Is there a way of doing this? const v1: Foo = 'A' // This should work const v2: Foo = 'D' // This should give me an error since 'D' doesn't exist in values ... const values = { A: 'A', B: 'B' } type Foo = keyof typeof values const v1: Foo = 'A' const v2: Foo = 'D' // Type '"D"' is not assignable to type '"A" | "B"' ... Link to playground http://www.typescriptlang.org/play/#src=let vals1 = ['A', 'B'] type Foo1 = OneOf<vals1> // Is there a way of doing this?
Author   G2Jose
🌐
Total TypeScript
totaltypescript.com › get-the-type-of-an-array-element
This Crazy Syntax Lets You Get An Array Element's Type | Total TypeScript
September 2, 2024 - First, we'll define an array of some fixture data we might use in some tests. ... We can use typeof on possibleResponses to extract its type. We can see that it is an array of objects with a status and body property.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › indexed-access-types.html
TypeScript: Documentation - Indexed Access Types
Another example of indexing with an arbitrary type is using number to get the type of an array’s elements. We can combine this with typeof to conveniently capture the element type of an array literal:
🌐
Webdevtutor
webdevtutor.net › blog › typescript-typeof-array-of-objects
Understanding TypeScript's `typeof` with an Array of Objects
We then use the forEach method to iterate over the array and log the type of each object using typeof. As expected, the output is "object", indicating that each object in the array is indeed an object. ... When working with arrays of objects in TypeScript, you may encounter other scenarios where typeof can be useful.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-array-typeof
JavaScript Typeof for Data Types: Array, Boolean and More | Built In
Yes, typeof returns "undefined" for undeclared variables without throwing an error. Use Array.isArray() or Object.prototype.toString.call() to accurately check if a JavaScript variable is an array data type, since typeof returns "object" for arrays.