-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update and improve typescript docs
- Loading branch information
Showing
5 changed files
with
105 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
title: Adding TypeScript | ||
--- | ||
|
||
[TypeScript](https://www.typescriptlang.org/) is a typed superset of JavaScript that compiles to plain JavaScript. | ||
|
||
Custom Applications have full support for developing applications in TypeScript. | ||
|
||
<Info> | ||
|
||
This feature is available from version `>= 21.8.0`. | ||
|
||
</Info> | ||
|
||
To start a new Custom Application project with TypeScript, you can use the TypeScript starter template: | ||
|
||
```console noPromptLines="2-3" | ||
npx @commercetools-frontend/create-mc-app@latest \ | ||
my-new-custom-application-project \ | ||
--template starter-typescript | ||
``` | ||
|
||
The TypeScript starter template is the same as the standard JS starter template in terms of functionality but it includes the additional TypeScript setup. | ||
|
||
# Configuration | ||
|
||
Every TypeScript project requires a `tsconfig.json` file to instruct TypeScript on how to compile the project. | ||
|
||
Therefore, we provide a base TSConfig to be used in your `tsconfig.json` file: | ||
|
||
```json | ||
{ | ||
"extends": "@commercetools-frontend/application-config/tsconfig-mc-app.json" | ||
} | ||
``` | ||
|
||
Furthermore, we provide a `client.d.ts` declaration file with some basic type shims for importing media assets: | ||
|
||
- `.mod.css` and `.module.css` | ||
- `.png` | ||
- `.svg` | ||
|
||
You can include this using the TypeScript triple-slash directives: | ||
|
||
```ts | ||
/// <reference types="@commercetools-frontend/application-config/client" /> | ||
``` | ||
|
||
<Info> | ||
|
||
By default, this is included in the TypeScript starter template `src/index.tsx` entry point file. | ||
|
||
</Info> | ||
|
||
You can also include this in the `tsconfig.json` file in the `compilerOptions.types` field but we don't recommend | ||
to use that unless you are very familiar with the [implications of using the `types` field](https://www.typescriptlang.org/tsconfig#types). | ||
|
||
Additional adjustments to other config files is also required, in particular: | ||
|
||
- `.prettierrc`: use the `typescript` parser. | ||
- `jest.test.config.js`: use the `@commercetools-frontend/jest-preset-mc-app/typescript` preset. | ||
- `jest.eslint.config.js`: include the file extensions `ts` and `tsx` in `moduleFileExtensions`, then `<rootDir>/**/*.ts` and `<rootDir>/**/*.tsx` in `testMatch`. | ||
|
||
# Migrate to TypeScript | ||
|
||
If you have an existing Custom Application project and would like to migrate it to TypeScript, you need to do the following: | ||
|
||
* Install the `typescript` dependency. | ||
* Add a `tsconfig.json` file. See [configuration](#configuration) for the tooling setup. | ||
* Migrate the JavaScript files to TypeScript, either `.ts` or `.tsx` (if a file contains JSX syntax). | ||
|
||
<Info> | ||
|
||
Migrating source files to TypeScript can be done incrementally. We recommend starting from the "leaf files" and work your way up to the application entry point. | ||
The [migrating from JavaScript documentation](https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html) can help with setting up a basic plan. | ||
|
||
All UIKit and ApplicationKit packages provide declaration files and export useful types when necessary. | ||
|
||
During the migration you might need to loosen up the types strictness, for example by allowing explicit `any` types. | ||
|
||
```json title="tsconfig.json" | ||
{ | ||
"extends": "@commercetools-frontend/application-config/tsconfig-mc-app.json", | ||
"compilerOptions": { | ||
"noExplicitAny": false, | ||
"strict": false | ||
} | ||
} | ||
``` | ||
|
||
Please remember to revert that once the migration is over, as using `any` should be avoided. | ||
|
||
</Info> | ||
|
||
* Add a script command `"typecheck": "tsc --noEmit"` in your `package.json` to compile the application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters