> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/advanced/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/advanced/type-script/mini-website-with-typescript-compile-file-ts-chay-thoi-gian-thuc-ok/khong-hieu-tai-sao-khi-su-dung-nodemon.json-no-moi-chay-duoc/property-does-not-exist-on-type-window-in-typescript-fixed.md).

# Property does not exist on type Window in TypeScript \[Fixed]

## Property does not exist on type Window in TypeScript \[Fixed]

Last updated: Feb 27, 2024\
Reading time·5 min

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#table-of-contents) Table of Contents

1. [Property does not exist on type Window in TypeScript](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#property-does-not-exist-on-type-window-in-typescript)
2. [Property 'X' does not exist on type 'typeof globalThis](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#property-x-does-not-exist-on-type-typeof-globalthis)

The article shows how to solve the error by extending the `Window` or `globalThis` interface.

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#property-does-not-exist-on-type-window-in-typescript) Property does not exist on type Window in TypeScript

**The "Property does not exist on type 'Window & typeof globalThis'" error occurs when we access a property that doesn't exist on the `Window` interface.**

**To solve the error, extend the `Window` interface in a `.d.ts` file and add the property you intend to access on the `window` object.**

Here is an example of how the error occurs:

index.ts

```typescript
// ⛔️ Property 'example' does not exist on
// type 'Window & typeof globalThis'.ts(2339)
window.example = 'bobbyhadz.com';

console.log(window.example);
```

![property does not exist on type window in typescript](https://bobbyhadz.com/images/blog/typescript-property-does-not-exist-on-type-window/property-does-not-exist-on-type-window-in-typescript.webp)

In your `src` directory, create a `types` directory that contains the following `index.d.ts` file:

src/types/index.d.ts

```typescript
export {};

declare global {
  interface Window {
    example: string;
  }
}
```

The code for this article is available on [GitHub](https://github.com/bobbyhadz/typescript-property-does-not-exist-on-type-window)The code sample shows how to extend the `Window` interface with a property named `example` that has a type of `string`. This will be different in your case, so make sure to adjust the property names and the types.

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#turn-off-type-checking-for-a-specific-property) Turn off type checking for a specific property

If you don't know the type of the specific property and want to turn off type checking, set it to `any`.

src/types/index.d.ts

```typescript
export {};

declare global {
  interface Window {
    example: any; // 👈️ turn off type checking
  }
}
```

The code for this article is available on [GitHub](https://github.com/bobbyhadz/typescript-property-does-not-exist-on-type-window)

Now I'm able to set and access the `example` property on the `window` object without getting the error.

index.ts

```typescript
// ✅ Works now
window.example = 'bobbyhadz.com';

console.log(window.example);
```

![accessing and setting properties on window in typescript](https://bobbyhadz.com/images/blog/typescript-property-does-not-exist-on-type-window/accessing-and-setting-properties-on-window-in-typescript.webp)

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#add-the-types-directory-to-your-tsconfigjson-file) Add the `types` directory to your `tsconfig.json` file

If the error persists, try adding the path to your `types` directory to your [tsconfig.json](https://bobbyhadz.com/blog/typescript-generate-tsconfig-json) file.

tsconfig.json

```json
{
  "compilerOptions": {
    // ... rest
    "typeRoots": ["./node_modules/@types", "./src/types"]
  }
}
```

We used the `export {}` line in our `index.d.ts` file to mark it as an external module.

A module is a file that contains at least 1 `import` or `export` statement, so we are required to do that to be able to augment the global scope.

Note that you will have to change the contents of the provided `index.d.ts` file according to your use case.

You should add the names (and types) of all of the properties you intend to access on the `window` object.

src/types/index.d.ts

```typescript
export {};

declare global {
  interface Window {
    example: string;
  }
}
```

The code for this article is available on [GitHub](https://github.com/bobbyhadz/typescript-property-does-not-exist-on-type-window)

The provided file simply adds an `example` property with a type of `string`, which is most likely not what you need.

TypeScript looks for `.d.ts` files in the same places it looks for your regular `.ts` files. The location is determined by the `include` and `exclude` settings in your`tsconfig.json` file.

TypeScript will merge the declared from you `Window` [interface](https://bobbyhadz.com/blog/typescript-extend-multiple-interfaces) with the original `Window` interface, so when you use the `window` object, you will be able to access properties from both interfaces.

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#using-an-inline-type-assertion) Using an inline type assertion

You can also use an inline type assertion.

index.ts

```typescript
const result = (window as any).myProperty;

console.log(result);
```

We used a type assertion in place to type the `window` object as any which enables us to access any property on the object without getting a type error.

However, this solution is hacky and should be avoided, unless you can't get anything else to work.

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#property-x-does-not-exist-on-type-typeof-globalthis) Property 'X' does not exist on type 'typeof globalThis'

**The error "Property 'X' does not exist on type 'typeof globalThis" occurs when we try to access a property that doesn't exist on the `global` object.**

**To solve the error, extend the global object and add types for the necessary properties.**

Here is an example of how the error occurs.

index.ts

```typescript
// ⛔️ Property 'hello' does not exist on type 'typeof globalThis'.
global.hello = 'bobbyhadz.com';
```

![property does not exist on type typeof globalthis](https://bobbyhadz.com/images/blog/typescript-property-does-not-exist-on-type-window/property-does-not-exist-on-type-typeof-globalthis.webp)

We tried to access a property that doesn't exist on the `global` object and got the error.

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#add-typings-for-the-properties-and-methods-you-need-to-access-on-global) Add typings for the properties and methods you need to access on `global`

We have to add typings for the properties and methods we intend to access on the `global` object.

In your `src` directory, create a `types` directory that contains the following `index.d.ts` file:

src/types/index.d.ts

```typescript
/* eslint-disable no-var */

declare global {
  var example: string;
  function sum(a: number, b: number): number;
}

export {};
```

We added an `example` property that has a type of `string` and a `sum` method.

Note that this will be different in your use case, so make sure to adjust the property names and the types.

Make sure to use the `var` keyword to add typings for properties you intend to set and use in other files.

You need to add the names and types of all of the properties you intend to access on the `global` object.

For example, if you don't know the type of the specific property and want to turn off type checking, set it to `any`.

src/types/index.d.ts

```typescript
/* eslint-disable no-var */

declare global {
  var example: string;
  function sum(a: number, b: number): number;
}

export {};
```

Now, I'm able to set and access the specified property on the `global` object without getting any errors.

index.ts

```typescript
global.example = 'hello world';

global.sum = function (a: number, b: number) {
  return a + b;
};

console.log(global.example); // 👉️ "hello world"

console.log(global.sum(15, 25)); // 👉️ 40
```

![setting and accessing properties on global object](https://bobbyhadz.com/images/blog/typescript-property-does-not-exist-on-type-window/setting-and-accessing-properties-on-global-object.webp)

Note that you might still be getting an error in your terminal [if you are using ts-node](https://github.com/TypeStrong/ts-node/issues/745).

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#solve-the-issue-with-ts-node) Solve the issue with `ts-node`

The problem is with `ts-node` not recognizing local [declaration files](https://bobbyhadz.com/blog/typescript-could-not-find-a-declaration-file-for-module).

To resolve this, use the `--files` flag with your `ts-node` command, so instead of `ts-node ./src/index.ts` you should run `ts-node --files ./src/index.ts`.

I use `nodemon` with `ts-node` and here are the contents of my `nodemon.json` file.

nodemon.json

```json
{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "ts-node --files ./src/index.ts"
}
```

After adding the `--files` flag (only required if using `ts-node`), restart your server and you should be good to go.

Note that this makes the `sum` function and the `example` property accessible directly (globally) and on the `global` object.

index.ts

```typescript
global.example = 'bobbyhadz.com';

global.sum = function (a: number, b: number) {
  return a + b;
};

console.log(global.example); // 👉️ "bobbyhadz.com"
console.log(global.sum(15, 25)); // 👉️ 40

console.log(example); // 👉️ "bobbyhadz.com"
console.log(sum(5, 15)); // 👉️ 20
```

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#add-the-path-to-your-types-directory-to-tsconfigjson) Add the path to your `types` directory to `tsconfig.json`

If you still get an error in your IDE, try adding the path to your `types` directory to your `tsconfig.json` file.

tsconfig.json

```json
{
  "compilerOptions": {
    // ... rest
    "typeRoots": ["./node_modules/@types", "./src/types"]
  }
}
```

We used the `export {}` line in our `index.d.ts` file to mark it as an external module. A module is a file that contains at least 1 `import` or `export` statement. We are required to do that to be able to augment the global scope.

Note that you will have to change the contents of the provided `index.d.ts` file according to your use case.

You should add the names (and types) of all of the properties you intend to access on the `global` object.

src/types/index.d.ts

```typescript
/* eslint-disable no-var */

declare global {
  var example: any; // 👈️ disables type checking for property
  function sum(a: number, b: number): number;
}

export {};
```

The provided file simply adds an `example` property with a type of `any` and a `sum` method, which is most likely not what you need.

TypeScript looks for `.d.ts` files in the same places it looks for your regular `.ts` files.

The location is determined by the `include` and `exclude` settings in your `tsconfig.json` file.

TypeScript will merge the typings you declared on the global object with the original typings, so you will be able to access properties and methods from both declarations.

### [#](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type-window#additional-resources) Additional Resources

You can learn more about the related topics by checking out the following tutorials:

* [Extend String.prototype and other prototypes in TypeScript](https://bobbyhadz.com/blog/typescript-extend-string-prototype)
* [How to extend Array.prototype in TypeScript](https://bobbyhadz.com/blog/typescript-array-extend)
* [Property does not exist on type '{}' in TypeScript](https://bobbyhadz.com/blog/typescript-property-does-not-exist-on-type)

I wrote [a book](https://bobbyhadz.com/blog/all-in-programming) in which I share everything I know about how to become a better, more efficient programmer.[![book cover](https://bobbyhadz.com/images/global/book-cover.webp)](https://bobbyhadz.com/blog/all-in-programming)You can use the search field on my [Home Page](https://bobbyhadz.com/) to filter through all of my articles.
