Skip to content

Commit

Permalink
Resolve react+vite lib type checking errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuzina committed Nov 18, 2024
1 parent 1ae0447 commit 4622eaa
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 53 deletions.
10 changes: 0 additions & 10 deletions apps/boilerplate-react-vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,3 @@ application using the standard set of shared Canonical packages.
#### Running the boilerplate

Run `bun run dev` to run a live development server.

### Live TS checking

[vite-plugin-checker](https://github.com/fi3ework/vite-plugin-checker) is used to
include Typescript type checking as a reload & build step.

To disable this, remove `checker()` from `vite.config.ts` and remove the
`vite-plugin-checker` dependency.
You may also remove `skipLibCheck` from `tsconfig.node.json`, as this is used
to ignore type-checking the checker's library dependencies.
3 changes: 1 addition & 2 deletions apps/boilerplate-react-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@vitejs/plugin-react": "^4.3.2",
"globals": "^15.9.0",
"typescript": "^5.6.2",
"vite": "^5.4.8",
"vite-plugin-checker": "^0.8.0"
"vite": "^5.4.8"
}
}
5 changes: 2 additions & 3 deletions apps/boilerplate-react-vite/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@canonical/typescript-config-react",
"extends": "@canonical/typescript-config-node",
"compilerOptions": {
"noEmit": true,
"skipLibCheck": true
"noEmit": true
},
"include": ["vite.config.ts"]
}
6 changes: 1 addition & 5 deletions apps/boilerplate-react-vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import checker from "vite-plugin-checker";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
checker({
typescript: { tsconfigPath: "tsconfig.app.json" },
}),
react()
],
});
7 changes: 0 additions & 7 deletions apps/ts_example/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
## TS Example

A simple example of using TS and Biome in a Typescript app.

## Caveats
### TSConfig
#### `lib` override

Some functions in this example (such as `setTimeout`) require the DOM API.
Therefore, this package includes the DOM lib, which is not included by the base Typescript config.
4 changes: 2 additions & 2 deletions apps/ts_example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@canonical/ds25-ts-test",
"private": true,
"version": "0.1.1-experimental.0",
"description": "TypeScript test project",
"description": "Typescript test project",
"type": "module",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
Expand Down Expand Up @@ -36,6 +36,6 @@
"devDependencies": {
"@biomejs/biome": "^1.9.2",
"@canonical/biome-config": "^0.1.1-experimental.0",
"@canonical/typescript-config-base": "^0.1.1-experimental.0"
"@canonical/typescript-config-node": "^0.0.0-experimental.0"
}
}
2 changes: 1 addition & 1 deletion apps/ts_example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@canonical/typescript-config-base",
"extends": "@canonical/typescript-config-node",
"compilerOptions": {
"baseUrl": "src"
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
33 changes: 33 additions & 0 deletions configs/typescript-node/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Canonical Typescript Configuration

This package provides a central configuration for Canonical's Node TypeScript projects.

### Installation

#### Bun

```bash
bun add -d @canonical/typescript-config-node
```

#### NPM

```bash
npm install --save-dev @canonical/typescript-config-node
```

#### Yarn

```bash
yarn add --dev @canonical/typescript-config-node
```

### Usage

Create a `tsconfig.json` file in the root of your project and extend the configuration.

```json
{
"extends": "@canonical/typescript-config-node"
}
```
36 changes: 36 additions & 0 deletions configs/typescript-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@canonical/typescript-config-node",
"version": "0.0.0-experimental.0",
"description": "Canonical's standard TypeScript configuration for Node-based projects",
"main": "tsconfig.json",
"author": {
"email": "[email protected]",
"name": "Canonical Webteam"
},
"repository": {
"type": "git",
"url": "https://github.com/canonical/ds25"
},
"license": "LGPL-3.0",
"bugs": {
"url": "https://github.com/canonical/ds25/issues"
},
"homepage": "https://github.com/canonical/ds25#readme",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"check": "bun run check:biome",
"check:fix": "bun run check:biome:fix",
"check:biome": "biome check *.json",
"check:biome:fix": "biome check --write *.json"
},
"devDependencies": {
"typescript": "^5.6.2"
},
"peerDependencies": {
"typescript": "^5.6.2"
},
"dependencies": {
"@types/node": "^22.9.0",
"@canonical/typescript-config-base": "^0.1.1-experimental.0"
}
}
6 changes: 6 additions & 0 deletions configs/typescript-node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@canonical/typescript-config-base",
"compilerOptions": {
"lib": ["ES2023", "DOM"]
}
}
12 changes: 6 additions & 6 deletions configs/typescript-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"typescript": "^5.6.2"
},
"peerDependencies": {
"typescript": "^5.6.2",
"react": "^19.0.0-rc-1460d67c-20241003",
"react-dom": "^19.0.0-rc-1460d67c-20241003",
"@types/react": "npm:types-react@rc",
"@types/react-dom": "npm:types-react-dom@rc"
"react": "^18.3.1 || ^19.0.0",
"react-dom": "^18.3.1 || ^19.0.0",
"typescript": "^5.6.2"
},
"dependencies": {
"@canonical/typescript-config-base": "^0.1.1-experimental.0"
"@types/react": "^18.3.12 || npm:types-react-dom@rc",
"@types/react-dom": "^18.3.1 || npm:types-react-dom@rc",
"@canonical/typescript-config-node": "^0.0.0-experimental.0"
}
}
5 changes: 3 additions & 2 deletions configs/typescript-react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "@canonical/typescript-config-base",
"extends": "@canonical/typescript-config-node",
"compilerOptions": {
"lib": ["ES2023", "DOM"],
"module": "ESNext",
"moduleResolution": "Bundler",
"jsx": "react-jsx"
}
}
11 changes: 0 additions & 11 deletions packages/ds-react-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ Currently, two official plugins are available:

### TSConfig

#### Skip library check

We use [skipLibCheck](https://www.typescriptlang.org/tsconfig/#skipLibCheck) to
skip type checking of declaration files. This is needed for compatibility with
vite dependencies. If this option is not enabled, the following error occurs:

```
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("storybook/internal/types")' call instead.
```


### Bun

[Bun](https://bun.sh/) is being experimentally used as a package manager.
Expand Down
3 changes: 1 addition & 2 deletions packages/ds-react-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@canonical/typescript-config-react",
"compilerOptions": {
"baseUrl": "src",
"skipLibCheck": true
"baseUrl": "src"
},
"include": [
"src/**/*.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"devDependencies": {
"@biomejs/biome": "^1.9.2",
"@canonical/biome-config": "^0.1.0-experimental.0",
"@canonical/typescript-config-base": "^0.1.0-experimental.0",
"@canonical/typescript-config-node": "^0.0.0-experimental.0",
"typescript": "^5.5.3"
}
}
2 changes: 1 addition & 1 deletion packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@canonical/typescript-config-base",
"extends": "@canonical/typescript-config-node",
"compilerOptions": {
"baseUrl": "src"
},
Expand Down

0 comments on commit 4622eaa

Please sign in to comment.