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>
This commit is contained in:
Arnaud Barré
2023-12-24 15:13:53 +01:00
committed by GitHub
parent a119e8d636
commit 6077ace528
8 changed files with 47 additions and 65 deletions

View File

@@ -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
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
$ 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
}
}
```

View File

@@ -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"
}
}
}

View File

@@ -17,9 +17,9 @@
"mime"
],
"devDependencies": {
"bun-types": "latest"
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
}

View File

@@ -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);
}

View File

@@ -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
},
}

View File

@@ -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",

View File

@@ -3,7 +3,7 @@
"module": "index.ts",
"type": "module",
"devDependencies": {
"bun-types": "latest"
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "5.3.2"

View File

@@ -3,7 +3,7 @@
"module": "index.ts",
"type": "module",
"devDependencies": {
"bun-types": "latest"
"@types/bun": "latest"
},
"dependencies": {
"es-module-lexer": "1.3.0"