Skip to content

Tsconfig zh #1268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
display: "Linter Checks"
---

A collection of extra checks, which somewhat cross the boundaries of compiler vs linter. You may prefer to use a tool like <a href="https://github.com/typescript-eslint/typescript-eslint#typescript-eslint">eslint</a> over these options if you are looking for more in-depth rules.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
display: "Advanced"
---

Flags which help with debugging
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
display: "Project Options"
---

These settings are used to define the runtime expectations of your project, how and where you want the JavaScript to be emitted and the level of integration you want with existing JavaScript code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
display: "Command Line"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
display: "Experimental"
---

TypeScript strives to only include features which are confirmed to be added into the JavaScript language.

There have been cases where a feature is compelling enough to be an exception to that rule, and these live as experimental compiler flags.
It is possible that a version of these features may be different when/if they are added to the JavaScript language, and thus are considered risky.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
display: "模块解析"
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
display: "File Inclusion"
---

These settings help you ensure that TypeScript picks up the right files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
display: "Source Maps"
---

In order to provide rich debugging tools and crash reports which make sense to developers, TypeScript supports
emitting additional files which conform to the JavaScript Source Map standards.

These are emitted as `.map` files which live alongside the file they represent.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
display: "Strict Checks"
---

We recommend using the [compiler option `strict`](#strict) to opt-in to every possible improvement as they are built.

TypeScript supports a wide spectrum of JavaScript patterns and defaults to allowing for quite a lot of flexibility in accommodating these styles.
Often the safety and potential scalability of a codebase can be at odds with some of these techniques.

Because of the variety of supported JavaScript, upgrading to a new version of TypeScript can uncover two types of errors:

- Errors which already exist in your codebase, which TypeScript has uncovered because the language has refined its understanding of JavaScript.
- A new suite of errors which tackle a new problem domain.

TypeScript will usually add a compiler flag for the latter set of errors, and by default these are not enabled.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
display: "Watch Options"
---

TypeScript 3.8 shipped a new strategy for watching directories, which is crucial for efficiently picking up changes to `node_modules`.

On operating systems like Linux, TypeScript installs directory watchers (as opposed to file watchers) on `node_modules` and many of its subdirectories to detect changes in dependencies.
This is because the number of available file watchers is often eclipsed by the of files in `node_modules`, whereas there are way fewer directories to track.

Because every project might work better under different strategies, and this new approach might not work well for your workflows, TypeScript 3.8 introduces a new `watchOptions` field which allows users to tell the compiler/language service which watching strategies should be used to keep track of files and directories.
6 changes: 6 additions & 0 deletions packages/tsconfig-reference/copy/zh/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Intro to the TSConfig Reference

A TSConfig file in a directory indicates that the directory is the root of a TypeScript or JavaScript project.
The TSConfig file can be either a `tsconfig.json` or `jsconfig.json`, both have the same behavior and the same set of config variables.

This page covers all of the different flags available inside a TSConfig file. It starts with an overview of every flag, then moves into the root attributes in the JSON file, then the `compilerOptions` which are the bulk of the options and wraps up with `watchOptions`.
39 changes: 39 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/allowJs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
display: "Allow JS"
oneline: "Let TS include .JS files in imports"
---

Allow JavaScript files to be imported inside your project, instead of just `.ts` and `.tsx` files. For example, this JS file:

```js twoslash
// @filename: card.js
export const defaultCardDeck = "Heart";
```

When imported into a TypeScript file will raise an error:

```ts twoslash
// @errors: 2307
// @filename: card.js
module.exports.defaultCardDeck = "Heart";
// ---cut---
// @filename: index.ts
import { defaultCardDeck } from "./card";

console.log(defaultCardDeck);
```

Imports fine with `allowJs` enabled:

```ts twoslash
// @filename: card.js
module.exports.defaultCardDeck = "Heart";
// ---cut---
// @allowJs
// @filename: index.ts
import { defaultCardDeck } from "./card";

console.log(defaultCardDeck);
```

This flag can be used as a way to incrementally add TypeScript files into JS projects by allowing the `.ts` and `.tsx` files to live along-side existing JavaScript files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
display: "允许合成默认导入"
oneline: "当模块没有默认导入时,允许 'import x from y'"
---

当设置为 true, 并且模块**没有**显式指定默认导出时,`allowSyntheticDefaultImports` 可以让你这样写导入:

```ts
import React from "react";
```

而不是:

```ts
import * as React from "react";
```

例如:`allowSyntheticDefaultImports` 不为 true 时:

```ts twoslash
// @errors: 1259
// @checkJs
// @allowJs
// @esModuleInterop: false
// @filename: utilFunctions.js
// @noImplicitAny: false
const getStringLength = (str) => str.length;

module.exports = {
getStringLength,
};

// @filename: index.ts
import utils from "./utilFunctions";

const count = utils.getStringLength("Check JS");
```

这段代码会引发一个错误,因为没有“default”对象可以导入,即使你认为应该有。
为了使用方便,Babel 这样的转译器会在没有默认导出时自动为其创建,使模块看起来更像:


```js
// @filename: utilFunctions.js
const getStringLength = (str) => str.length;
const allFunctions = {
getStringLength,
};

module.exports = allFunctions;
module.exports.default = allFunctions;
```

本选项不会影响 TypeScript 生成的 JavaScript,它仅对类型检查起作用。当你使用 Babel 生成额外的默认导出,从而使模块的默认导出更易用时,本选项可以让 TypeScript 的行为与 Babel 一致。
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
display: "Allow Umd Global Access"
oneline: "Assume UMD imports are all globally available"
---

When set to true, `allowUmdGlobalAccess` lets you access UMD exports as globals from inside module files. A module file is a file that has imports and/or exports. Without this flag, using an export from a UMD module requires an import declaration.

An example use case for this flag would be a web project where you know the particular library (like jQuery or Lodash) will always be available at runtime, but you can’t access it with an import.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
display: "Allow Unreachable Code"
oneline: "Error when code will never be called"
---

When:

- `undefined` (default) provide suggestions as warnings to editors
- `true` unreachable code is ignored
- `false` raises compiler errors about unreachable code

These warnings are only about code which is provably unreachable due to the use of JavaScript syntax, for example:

```ts
function fn(n: number) {
if (n > 5) {
return true;
} else {
return false;
}
return true;
}
```

With `"allowUnreachableCode": false`:

```ts twoslash
// @errors: 7027
// @allowUnreachableCode: false
function fn(n: number) {
if (n > 5) {
return true;
} else {
return false;
}
return true;
}
```

This does not affect errors on the basis of code which _appears_ to be unreachable due to type analysis.
19 changes: 19 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/allowUnusedLabels.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
display: "Allow Unused Labels"
oneline: "Error when accidentally creating a label"
---

Set to false to disable warnings about unused labels.

Labels are very rare in JavaScript and typically indicate an attempt to write an object literal:

```ts twoslash
// @errors: 7028
// @allowUnusedLabels: false
function verifyAge(age: number) {
// Forgot 'return' statement
if (age > 18) {
verified: true;
}
}
```
8 changes: 8 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/alwaysStrict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
display: "Always Strict"
oneline: "Ensure 'use strict' is always emitted"
---

Ensures that your files are parsed in the ECMAScript strict mode, and emit "use strict" for each source file.

[ECMAScript strict](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Strict_mode) mode was introduced in ES5 and provides behavior tweaks to the runtime of the JavaScript engine to improve performance, and makes a set of errors throw instead of silently ignoring them.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
display: "Assume Changes Only Affect Direct Dependencies"
oneline: "A drastically faster, but occasionally inaccurate watch mode option."
---

When this option is enabled, TypeScript will avoid rechecking/rebuilding all truly possibly-affected files, and only recheck/rebuild files that have changed as well as files that directly import them.

This can be considered a 'fast & loose' implementation of the watching algorithm, which can drastically reduce incremental rebuild times at the expense of having to run the full build occasionally to get all compiler error messages.
27 changes: 27 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/baseUrl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
display: "Base Url"
oneline: "Set a baseurl for relative module names"
---

Lets you set a base directory to resolve non-absolute module names.

You can define a root folder where you can do absolute file resolution. E.g.

```
baseUrl
├── ex.ts
├── hello
│ └── world.ts
└── tsconfig.json
```

With `"baseUrl": "./"` inside this project TypeScript will look for files starting at the same folder as the `tsconfig.json`.

```ts
import { helloWorld } from "hello/world";

console.log(helloWorld);
```

If you get tired of imports always looking like `"../"` or `"./"`. Or needing
to change as you move files, this is a great way to fix that.
7 changes: 7 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/charset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
display: "Charset"
oneline: "Manually set the text encoding for reading files"
---

In prior versions of TypeScript, this controlled what encoding was used when reading text files from disk.
Today, TypeScript assumes UTF-8 encoding, but will correctly detect UTF-16 (BE and LE) or UTF-8 BOMs.
40 changes: 40 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/checkJs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
display: "Check JS"
oneline: "Run the type checker on .js files in your project"
---

Works in tandem with `allowJs`. When `checkJs` is enabled then errors are reported in JavaScript files. This is
the equivalent of including `// @ts-check` at the top of all JavaScript files which are included in your project.

For example, this is incorrect JavaScript according to the `parseFloat` type definition which comes with TypeScript:

```js
// parseFloat only takes a string
module.exports.pi = parseFloat(3.124);
```

When imported into a TypeScript module:

```ts twoslash
// @allowJs
// @filename: constants.js
module.exports.pi = parseFloat(3.124);

// @filename: index.ts
import { pi } from "./constants";
console.log(pi);
```

You will not get any errors. However, if you turn on `checkJs` then you will get error messages from the JavaScript file.

```ts twoslash
// @errors: 2345
// @allowjs: true
// @checkjs: true
// @filename: constants.js
module.exports.pi = parseFloat(3.124);

// @filename: index.ts
import { pi } from "./constants";
console.log(pi);
```
17 changes: 17 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/composite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
display: "Composite"
oneline: "Used to create multiple build projects"
---

The `composite` option enforces certain constraints which make it possible for build tools (including TypeScript
itself, under `--build` mode) to quickly determine if a project has been built yet.

When this setting is on:

- The `rootDir` setting, if not explicitly set, defaults to the directory containing the `tsconfig.json` file.

- All implementation files must be matched by an `include` pattern or listed in the `files` array. If this constraint is violated, `tsc` will inform you which files weren't specified.

- `declaration` defaults to `true`

You can find documentation on TypeScript projects in [the handbook](https://www.typescriptlang.org/docs/handbook/project-references.html).
32 changes: 32 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
display: "Declaration"
oneline: "Emit d.ts files for referenced files in the project"
---

Generate `.d.ts` files for every TypeScript or JavaScript file inside your project.
These `.d.ts` files are type definition files which describe the external API of your module.
With `.d.ts` files, tools like TypeScript can provide intellisense and accurate types for un-typed code.

When `declaration` is set to `true`, running the compiler with this TypeScript code:

```ts twoslash
export let helloWorld = "hi";
```

Will generate an `index.js` file like this:

```ts twoslash
// @showEmit
export let helloWorld = "hi";
```

With a corresponding `helloWorld.d.ts`:

```ts twoslash
// @showEmittedFile: index.d.ts
// @showEmit
// @declaration
export let helloWorld = "hi";
```

When working with `.d.ts` files for JavaScript files you may want to use [`emitDeclarationOnly`](#emitDeclarationOnly) or use [`outDir`](#outDir) to ensure that the JavaScript files are not overwritten.
Loading