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 OverflowIn Typescript how do you get an element type from an array type, when the array contains different element types? - Stack Overflow
typescript - How to get type of array items? - Stack Overflow
Deriving a type from fields of objects in an array
Creating types from values in array
Videos
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 [...]
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" }
];
You can use this format to get the member type:
type InputType = typeof input[number];
You can read more about it (and some other cool tips) here in Steve Holgado's blog article.
You can extract it by pointing at an array member, so while you are using the below code to get the type of the array:
type A = typeof input;
You can use this to get the type of a single item:
type B = typeof input[0];
If you're looking to how to extract { name: string; test: number; } type, you can simply create alias to "item at index":
type Foo = Array<{ name: string; test: number; }>;
type FooItem = Foo[0];
or
type FooItem = Foo[number];
Starting with TypeScript 2.8 you can also do this with the infer keyword. This should only be used for more complicated cases (arrays nested in promises, etc), otherwise it is overly verbose.
type GetElementType<T extends any[]> = T extends (infer U)[] ? U : never;
For example:
// ElementType === string
type ElementType = string[] extends (infer U)[] ? U : never;
The infer keyword is very powerful and can extract any type out of larger type. For example if the type was a function that returns an array:
type FncType = () => [{ name: string }];
// Output === { name: string }
type Output = FncType extends () => (infer U)[] ? U : never;
You can also use the infer keyword in generic types:
type GetArrayReturnType<T> = T extends () => (infer U)[] ? U : never;
// Output === { name: string }
type Output = GetArrayReturnType<() => [{ name: string }]>;
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 = stringIs there a way I can have my cake and eat it, here?
typescript playground
TIA!