Question: I am working with typescript for the first time and here is my problem.

I have the following class:

```
class FileSystemStatistics{
    public number: number;
    public name: string[];
    public size: number;
    public used: number;
    public usedPercent: number;
    public physicalLocation: string;
}

```

I analyze the PC for hardrives and as expected it returns an array of them. For testing purpose I just made the name property an array of the type string. Now i simply want to assign name[0] the name of first harddrive, name[1] the name of second harddrive etc.

I am doing the following to achieve this:

```
var fsObject = new FileSystemStatistics();
//testing if HDs are returned as an array consisting of objects. fs is the name (e.g. C:) 
console.log(fsSize[0].fs); //--> C:
console.log(fsSize[1].fs); //--> G:
fsObject.name[0] = fsSize[0].fs;
fsObject.name[0] = fsSize[0].fs;

```

There are no mistakes marked in my code but when I want to run it I get the error: 

> 
> "UnhandledPromiseRejectionWarning: TypeError: Cannot set property '0'
>  of undefined"
> 
> 
> 

I am pretty sure I am making a beginner mistake but I cannot solve it even after googling.

Answer: 
The problem is not actually in typescript, but in the way how you treat your class.
You have to manually assign empty array to your name property, so instead of 

```
class FileSystemStatistics{
    public number: number;
    public name: string[];
    public size: number;
    public used: number;
    public usedPercent: number;
    public physicalLocation: string;
}

```

it should be at least

```
class FileSystemStatistics{
    public number: number;
    public name: string[] = [];
    public size: number;
    public used: number;
    public usedPercent: number;
    public physicalLocation: string;
}

```

What you did with `public name: string[]` expression is that you only mark that name property should be an array (or undefined/void) but not an array with numbers for instance 

```
...
public name: string[] = [1,2,3]; // would gave an error

```

Try to check what code you will have at runtime in playground 
<https://www.typescriptlang.org/play/index.html>

Treat typescript as a superset of javascript, which only exists before compilation and generate javascript.
In terms of runtime code, which you actually tested and what gave you an error in your post - you had a plain function as constructor, which generate you an empty object

and what you tried to achieve is to use your empty object named `fsObject`
in expression like this

```
fsObject; // {}
fsObject.name // which is already undefined
fsObject.name[0] // this is where you get runtime error with undefined treated as array

```
The class `FileSystemStatistics` you've created don't have any initial value. Which means that `fsObject.name` is `undefined`. You can avoid this issue by providing an initial value to the `name` property.

```
class FileSystemStatistics{
    public name: string[] = [];
}

```

You can see the output inside [the TypeScript playground](https://www.typescriptlang.org/play/index.html#src=class%20FileSystemStatistics%7B%0D%0A%20%20%20%20public%20name%3A%20string%5B%5D%20%3D%20%5B%5D%3B%0D%0A%7D%0D%0A%0D%0Aconst%20cs%20%3D%20new%20FileSystemStatistics()%3B%0D%0A%0D%0Acs.name%5B0%5D%20%3D%20'Test'%3B).