# TypeScript

## TypeScript in the Homey CLI

[TypeScript](https://www.typescriptlang.org) enables you to add types to your code which may aid you when developing your app. The Homey CLI allows you to choose between JavaScript and TypeScript when creating a new app. It is also possible to convert an existing app written in Javascript to TypeScript. This can even be done on a file by file basis.

### How it works

TypeScript 'transpiles' to JavaScript. This means that the TypeScript you write will be converted to JavaScript that runs on Homey. When your app is being processed to run on a Homey, it is possible to invoke a TypeScript compiler as an extra build step. The compiler compiles your files to a folder called `.homeybuild/` in the root folder of your app. After this compilation, Homey will bundle all JavaScript files into one app.

#### Creating new drivers with TypeScript

When the file `tsconfig.json` is present in the root folder of your app, the Homey CLI will default to TypeScript when running `homey app driver create`. If you want to avoid this, remove or rename `tsconfig.json` file.

{% hint style="warning" %}
Removing or remaning the `tsconfig.json` file will also prevent the TypeScript compiler from being invoked when running/installing/publishing your app.
{% endhint %}

## Creating a new app using TypeScript

Run `homey app create` in your terminal and answer 'Yes' when the CLI asks you to initialize your app with TypeScript utilities. All necessary and recommended dependencies and files will be created for you.

## Converting an existing app to TypeScript

If you have already created your app using JavaScript, but you want to convert your app to TypeScript, then this can only be performed manually. However, the conversion to TypeScript can be done file-by-file using the following steps:

#### Create a .tsconfig.json

The first thing to do is adding a file named `.tsconfig.json` to the root folder of your App. This will make Homey recognize your app as a TypeScript app. You are free to configure `tsconfig.json` to your liking. Only exception being the `outDir` which should remain `.homeybuild/`and having `sourceMap: true` is strongly recommended.

{% code title="/tsconfig.json" %}

```javascript
{
  "extends": "@tsconfig/node12/tsconfig.json",
  "compilerOptions": {
    "outDir": ".homeybuild/",
    "sourceMap": true
  }
}
```

{% endcode %}

#### Install dependencies

Make sure you have the file `.tsconfig.json` present in the root folder of your app. Then run the following command to install all necessary dependencies.

```
homey app add-types
```

#### Change your App entrypoint:

Rename app.js to app.ts and add the following lines at the top (You can remove `'use strict'`).

{% code title="/app.ts" %}

```
import sourceMapSupport from 'source-map-support';
sourceMapSupport.install();
...
```

{% endcode %}

#### Add the build step

Add a TypeScript transpile step in the npm `build` script.

{% code title="/package.json" %}

```
  "scripts": {
    "build": "tsc"
  },
```

{% endcode %}

#### All set!

Now you're all set! Run your app with `homey app run`. If you performed all steps correctly, then you should see a `Compiling TypeScript...` prompt before your app is being run.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apps.developer.homey.app/guides/tools/typescript.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
