From 6077ace52809886c5bc2f4206bcf6aa0b0b4af4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Sun, 24 Dec 2023 15:13:53 +0100 Subject: [PATCH] Update init template & TS documentation (#7813) * Update init template & TS documentation * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- docs/guides/runtime/typescript.md | 61 ++++++------------- packages/bun-inspector-frontend/package.json | 4 +- packages/bun-vscode/example/package.json | 4 +- src/cli/init_command.zig | 2 +- src/cli/tsconfig-for-init.json | 33 +++++----- test/cli/init/init.test.ts | 4 +- test/integration/sharp/package.json | 2 +- .../third_party/es-module-lexer/package.json | 2 +- 8 files changed, 47 insertions(+), 65 deletions(-) diff --git a/docs/guides/runtime/typescript.md b/docs/guides/runtime/typescript.md index f6afe02c1e..bb0ce14092 100644 --- a/docs/guides/runtime/typescript.md +++ b/docs/guides/runtime/typescript.md @@ -2,31 +2,10 @@ name: Install TypeScript declarations for Bun --- -To install TypeScript definitions for Bun's built-in APIs in your project, install `bun-types`. +To install TypeScript definitions for Bun's built-in APIs in your project, install `@types/bun`. ```sh -$ bun add -d bun-types # dev dependency -``` - ---- - -Then include `"bun-types"` in the `compilerOptions.types` in your `tsconfig.json`: - -```json-diff - { - "compilerOptions": { -+ "types": ["bun-types"] - } - } -``` - ---- - -Unfortunately, setting a value for `"types"` means that TypeScript will ignore other global type definitions, including `lib: ["dom"]`. If you need to add DOM types into your project, add the following [triple-slash directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) at the top of any TypeScript file in your project. - -```ts -/// -/// +$ bun add -d @types/bun # dev dependency ``` --- @@ -36,30 +15,30 @@ Below is the full set of recommended `compilerOptions` for a Bun project. With t ```jsonc { "compilerOptions": { - // add Bun type definitions - "types": ["bun-types"], - // enable latest features - "lib": ["esnext"], - "module": "esnext", - "target": "esnext", - - // if TS 5.x+ - "moduleResolution": "bundler", - "noEmit": true, - "allowImportingTsExtensions": true, + "lib": ["ESNext"], + "target": "ESNext", + "module": "ESNext", "moduleDetection": "force", - // if TS 4.x or earlier - // "moduleResolution": "nodenext", - "jsx": "react-jsx", // support JSX "allowJs": true, // allow importing `.js` from `.ts` - "esModuleInterop": true, // allow default imports for CommonJS modules - // best practices + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices "strict": true, - "forceConsistentCasingInFileNames": true, - "skipLibCheck": true + "skipLibCheck": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags + "useUnknownInCatchVariables": true, + "noPropertyAccessFromIndexSignature": true } } ``` diff --git a/packages/bun-inspector-frontend/package.json b/packages/bun-inspector-frontend/package.json index 6421ae745a..025f68aacb 100644 --- a/packages/bun-inspector-frontend/package.json +++ b/packages/bun-inspector-frontend/package.json @@ -3,7 +3,7 @@ "module": "index.ts", "type": "module", "devDependencies": { - "bun-types": "latest" + "@types/bun": "latest" }, "peerDependencies": { "typescript": "^5.0.0" @@ -11,4 +11,4 @@ "dependencies": { "esbuild": "^0.19.2" } -} \ No newline at end of file +} diff --git a/packages/bun-vscode/example/package.json b/packages/bun-vscode/example/package.json index eed10159db..f9a909752b 100644 --- a/packages/bun-vscode/example/package.json +++ b/packages/bun-vscode/example/package.json @@ -17,9 +17,9 @@ "mime" ], "devDependencies": { - "bun-types": "latest" + "@types/bun": "latest" }, "peerDependencies": { "typescript": "^5.0.0" } -} \ No newline at end of file +} diff --git a/src/cli/init_command.zig b/src/cli/init_command.zig index ae4cd6b53d..f66017ab65 100644 --- a/src/cli/init_command.zig +++ b/src/cli/init_command.zig @@ -317,7 +317,7 @@ pub const InitCommand = struct { if (needs_dev_dependencies) { var dev_dependencies = fields.object.get("devDependencies") orelse js_ast.Expr.init(js_ast.E.Object, js_ast.E.Object{}, logger.Loc.Empty); - try dev_dependencies.data.e_object.putString(alloc, "bun-types", "latest"); + try dev_dependencies.data.e_object.putString(alloc, "@types/bun", "latest"); try fields.object.put(alloc, "devDependencies", dev_dependencies); } diff --git a/src/cli/tsconfig-for-init.json b/src/cli/tsconfig-for-init.json index 7556e1d4b0..53eabed079 100644 --- a/src/cli/tsconfig-for-init.json +++ b/src/cli/tsconfig-for-init.json @@ -1,22 +1,25 @@ { "compilerOptions": { "lib": ["ESNext"], - "module": "esnext", - "target": "esnext", - "moduleResolution": "bundler", + "target": "ESNext", + "module": "ESNext", "moduleDetection": "force", - "allowImportingTsExtensions": true, - "noEmit": true, - "composite": true, - "strict": true, - "downlevelIteration": true, - "skipLibCheck": true, "jsx": "react-jsx", - "allowSyntheticDefaultImports": true, - "forceConsistentCasingInFileNames": true, "allowJs": true, - "types": [ - "bun-types" // add Bun global - ] - } + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + /* Linting */ + "skipLibCheck": true, + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "useUnknownInCatchVariables": true, + "noPropertyAccessFromIndexSignature": true + }, } diff --git a/test/cli/init/init.test.ts b/test/cli/init/init.test.ts index 2a6c2b1028..d876b97d16 100644 --- a/test/cli/init/init.test.ts +++ b/test/cli/init/init.test.ts @@ -22,7 +22,7 @@ test("bun init works", () => { "module": "index.ts", "type": "module", "devDependencies": { - "bun-types": "latest", + "@types/bun": "latest", }, "peerDependencies": { "typescript": "^5.0.0", @@ -58,7 +58,7 @@ test("bun init with piped cli", () => { "module": "index.ts", "type": "module", "devDependencies": { - "bun-types": "latest", + "@types/bun": "latest", }, "peerDependencies": { "typescript": "^5.0.0", diff --git a/test/integration/sharp/package.json b/test/integration/sharp/package.json index b1892aae51..3f6eef6f48 100644 --- a/test/integration/sharp/package.json +++ b/test/integration/sharp/package.json @@ -3,7 +3,7 @@ "module": "index.ts", "type": "module", "devDependencies": { - "bun-types": "latest" + "@types/bun": "latest" }, "peerDependencies": { "typescript": "5.3.2" diff --git a/test/js/third_party/es-module-lexer/package.json b/test/js/third_party/es-module-lexer/package.json index 4d2194ec26..b5486ed423 100644 --- a/test/js/third_party/es-module-lexer/package.json +++ b/test/js/third_party/es-module-lexer/package.json @@ -3,7 +3,7 @@ "module": "index.ts", "type": "module", "devDependencies": { - "bun-types": "latest" + "@types/bun": "latest" }, "dependencies": { "es-module-lexer": "1.3.0"