|
| 1 | +--- |
| 2 | +title: TypeScript template |
| 3 | +--- |
| 4 | + |
| 5 | +We know you like [TypeScript](https://www.typescriptlang.org/). We’ve seen how often you search for TypeScript on the [Enhance](https://enhance.dev) documentation site. We’ve heard from users on our [Discord](https://enhance.dev/discord) that they want a way to work with TypeScript in their Enhance projects. |
| 6 | + |
| 7 | +The thing is, you’ve always been able to use TypeScript with Enhance. The reason we haven’t officially described a way to set up an Enhance project with TypeScript before is because TypeScript configurations are a matter of taste and we didn’t want to give folks the impression that this is the only way. |
| 8 | + |
| 9 | +However, this has given a number of folks the impression that you cannot use TypeScript with Enhance and we wanted to dispel that myth. According to one user we were giving serious “not a real project” vibes by not supporting TypeScript. |
| 10 | + |
| 11 | +So today, we are introducing the [Enhance TypeScript Starter](https://github.com/enhance-dev/enhance-starter-typescript). |
| 12 | + |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +Assuming you are starting a new Enhance project you would run the command: |
| 17 | + |
| 18 | +```bash |
| 19 | +npx "@enhance/cli@latest" new ./myproject \ |
| 20 | + --template https://github.com/enhance-dev/enhance-starter-typescript -y |
| 21 | +``` |
| 22 | + |
| 23 | +This will set up a new Enhance project where you will code your APIs, elements and pages in TypeScript instead of JavaScript. Instead of editing files in the `app` folder you will do your editing in the `ts` folder. |
| 24 | + |
| 25 | + |
| 26 | +### Project Structure |
| 27 | + |
| 28 | +``` \ |
| 29 | +ts |
| 30 | +├── api ............... data routes |
| 31 | +│ └── index.mts |
| 32 | +├── browser ........... browser JavaScript |
| 33 | +│ └── index.mts |
| 34 | +├── components ........ single file web components |
| 35 | +│ └── my-card.mts |
| 36 | +├── elements .......... custom element pure functions |
| 37 | +│ └── my-header.mts |
| 38 | +├── pages ............. file-based routing |
| 39 | +│ └── index.html |
| 40 | +└── head.mts .......... custom <head> component |
| 41 | +``` |
| 42 | + |
| 43 | +Note: We are using `.mts` to tell the TypeScript Compiler to generate ES Modules as `.mjs` files.. |
| 44 | + |
| 45 | + |
| 46 | +## Local Development |
| 47 | + |
| 48 | +Running the local development environment is the same as any other Enhance project. The new `@enhance/plugin-typescript` is responsible for watching the `ts` folder for any file changes. If the file has an `.mts` extension they are re-compiled with the compilation target being the `app` folder. All other file types are simply copied to their corresponding locations in the `app` folder. |
| 49 | + |
| 50 | +## Authoring Code |
| 51 | + |
| 52 | +Write your code in TypeScript. We already have [types](https://github.com/enhance-dev/types) that you can import into your elements: |
| 53 | + |
| 54 | +<begin-code filename="ts/api/todo-item.mts"> |
| 55 | + |
| 56 | +```typescript |
| 57 | +import type { EnhanceElemArg } from "@enhance/types" |
| 58 | + |
| 59 | +export default ({ html, state: { attrs } }: EnhanceElemArg) => { |
| 60 | + const { state = "" } = attrs |
| 61 | + return html` |
| 62 | + ${state === "complete" ? "☑" : "☐"} |
| 63 | + <slot></slot> |
| 64 | + ` |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +</begin-code> |
| 69 | + |
| 70 | +Or APIs: |
| 71 | + |
| 72 | +<begin-code filename="ts/api/todos.mts"> |
| 73 | + |
| 74 | +```typescript |
| 75 | +import type { |
| 76 | + EnhanceApiFn, |
| 77 | + EnhanceApiReq, |
| 78 | + EnhanceApiRes, |
| 79 | +} from "@enhance/types"; |
| 80 | + |
| 81 | +type Todo = { |
| 82 | + title: string; |
| 83 | + completed?: boolean; |
| 84 | +}; |
| 85 | + |
| 86 | +export const get: EnhanceApiFn = async function ( |
| 87 | + request: EnhanceApiReq, |
| 88 | +): Promise<EnhanceApiRes> { |
| 89 | + |
| 90 | + console.log(`Handling ${request.path}...`); |
| 91 | + |
| 92 | + const todos: Todo[] = [ |
| 93 | + { title: "todo 1", completed: false }, |
| 94 | + { title: "todo 2", completed: true }, |
| 95 | + { title: "todo 3" }, |
| 96 | + ]; |
| 97 | + |
| 98 | + const response: EnhanceApiRes = { |
| 99 | + json: { todos }, |
| 100 | + }; |
| 101 | + |
| 102 | + return response; |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +</begin-code> |
| 107 | + |
| 108 | +## Deploying |
| 109 | + |
| 110 | +Use the [`@begin/deploy`](https://begin.com/deploy/docs/workflows/deploying-code) package to deploy your application. Alternatively, you can write a GitHub Action to [deploy on every commit](https://github.com/enhance-dev/enhance-starter-typescript/blob/main/.github/workflows/CI.yml). |
0 commit comments