diff --git a/README.md b/README.md index 1f5c3e5d67ec..014b88e3249b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,10 @@ code . The main website for TypeScript, a Gatsby website which is statically deployed. +```sh +yarn start +``` + ## Playground A React component for the TypeScript playground base component. Not the one available on diff --git a/packages/tsconfig-reference/README.md b/packages/tsconfig-reference/README.md index 6dce136851e3..c1eb973407cd 100644 --- a/packages/tsconfig-reference/README.md +++ b/packages/tsconfig-reference/README.md @@ -27,5 +27,12 @@ If you'd like to create a new language: The TSConfig reference is created by a two step process: - Creating the JSON dump of all the useful info via [`./scripts/generateJSON.ts`](scripts/generateJSON.ts) which you can find in [`./data`](./data). +- A script which uses the JSON, and the copy to generate per-language markdown docs which are picked up by the typescriptlang-org Gatsby site at `http://localhost:8000/tsconfig` -- A script which uses the JSON, and the copy to generate per-language markdown docs which are meant to be embedded inside webpages `` +You can run these commands from the root of the repo: + +```sh +yarn workspace tsconfig-reference run generate-json + +yarn workspace tsconfig-reference run generate-markdown +``` diff --git a/packages/tsconfig-reference/copy/en/categories/Project_Files_0.md b/packages/tsconfig-reference/copy/en/categories/Project_Files_0.md new file mode 100644 index 000000000000..f5d4c6ec2363 --- /dev/null +++ b/packages/tsconfig-reference/copy/en/categories/Project_Files_0.md @@ -0,0 +1,5 @@ +--- +display: "Project File Inclusion" +--- + + diff --git a/packages/tsconfig-reference/copy/en/options/allowUnreachableCode.md b/packages/tsconfig-reference/copy/en/options/allowUnreachableCode.md index a0f1b00ec0f5..36567c42eb4f 100644 --- a/packages/tsconfig-reference/copy/en/options/allowUnreachableCode.md +++ b/packages/tsconfig-reference/copy/en/options/allowUnreachableCode.md @@ -2,4 +2,19 @@ display: "Allow Unreachable Code" --- -Do not report errors on unreachable code. +Disables warnings about unreachable code. +These warnings are only about code which is provably unreachable due to syntactic construction, like the example below. + +```ts +// @allowUnreachableCode: false +function fn(n: number) { + if (n > 5) { + return true; + } else { + return false; + } + return true; +} +``` + +TypeScript doesn't error on the basis of code which *appears* to be unreachable due to type analysis. diff --git a/packages/tsconfig-reference/copy/en/options/allowUnusedLabels.md b/packages/tsconfig-reference/copy/en/options/allowUnusedLabels.md index 2e3fc23559a8..fe5d7ef1ca9e 100644 --- a/packages/tsconfig-reference/copy/en/options/allowUnusedLabels.md +++ b/packages/tsconfig-reference/copy/en/options/allowUnusedLabels.md @@ -2,4 +2,16 @@ display: "Allow Unused Labels" --- -Do not report errors on unused labels. +Disables warnings about unused labels. +Labels are very rare in JavaScript and typically indicate an attempt to write an object literal: + +```ts +// @allowUnusedLabels: false +function f(a: number) { + // Forgot 'return' statement! + if (a > 10) + { + m: 0 + } +} +``` diff --git a/packages/tsconfig-reference/copy/en/options/charset.md b/packages/tsconfig-reference/copy/en/options/charset.md index 67c7c4f7101d..6539146bb303 100644 --- a/packages/tsconfig-reference/copy/en/options/charset.md +++ b/packages/tsconfig-reference/copy/en/options/charset.md @@ -2,4 +2,7 @@ display: "Charset" --- -The character set of the input files. +> ❌ **Deprecated:** This option does nothing. + +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. diff --git a/packages/tsconfig-reference/copy/en/options/composite.md b/packages/tsconfig-reference/copy/en/options/composite.md index 92805c3eb41d..80658918bb9b 100644 --- a/packages/tsconfig-reference/copy/en/options/composite.md +++ b/packages/tsconfig-reference/copy/en/options/composite.md @@ -2,4 +2,10 @@ display: "Composite" --- -Enable project compilation +The `composite` option enforces certain constraints that 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` diff --git a/packages/tsconfig-reference/copy/en/options/declarationMap.md b/packages/tsconfig-reference/copy/en/options/declarationMap.md index 8bc500043a51..d487622df8a0 100644 --- a/packages/tsconfig-reference/copy/en/options/declarationMap.md +++ b/packages/tsconfig-reference/copy/en/options/declarationMap.md @@ -2,4 +2,8 @@ display: "Declaration Map" --- -Generates a sourcemap for each corresponding '.d.ts' file. + +Generates a source map for `.d.ts` files that maps back to the original `.ts` source file. +This will allow editors such as VS Code to go to the original `.ts` file when using features like *Go to Definition*. + +You should strongly consider turning this on if you're using project references. diff --git a/packages/tsconfig-reference/copy/en/options/downlevelIteration.md b/packages/tsconfig-reference/copy/en/options/downlevelIteration.md index 07bef7ca13d2..88933339e4cf 100644 --- a/packages/tsconfig-reference/copy/en/options/downlevelIteration.md +++ b/packages/tsconfig-reference/copy/en/options/downlevelIteration.md @@ -2,4 +2,72 @@ display: "Downlevel Iteration" --- -Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. +**Default**: `false`. Has no effect if `target` is ES6 or newer. + +ECMAScript 6 added several new iteration primitives: the `for / of` loop (`for (el of arr)`), Array spread (`[a, ...b]`), argument spread (`fn(...args)`), and `Symbol.iterator`. +`--downlevelIteration` allows for these iteration primitives to be used more accurately in ES5 environments if a `Symbol.iterator` implementation is present. + +#### Example: Effects on `for / of` + +Without `downlevelIteration` on, a `for / of` loop on any object is downleveled to a traditional `for` loop: + +```ts +// @target: ES5 +// @showEmit +const str = "Hello!"; +for (const s of str) { + console.log(s); +} +``` + +This is often what people expect, but it's not 100% compliant with ECMAScript 6 behavior. +Certain strings, such as emoji (😜), have a `.length` of 2 (or even more!), but should iterate as 1 unit in a `for-of` loop. +See [this blog post by Jonathan New](https://blog.jonnew.com/posts/poo-dot-length-equals-two) for a longer explanation. + +When `downlevelIteration` is enabled, TypeScript will use a helper function that checks for a `Symbol.iterator` implementation (either native or polyfill). +If this implementation is missing, you'll fall back to index-based iteration. + +```ts +// @target: ES5 +// @downlevelIteration +// @showEmit +const str = "Hello!"; +for (const s of str) { + console.log(s); +} +``` + +>> **Note:** Remember, `downlevelIteration` does not improve compliance if `Symbol.iterator` is not present! + +#### Example: Effects on Array Spreads + +This is an array spread: + +```js +// Make a new array who elements are 1 followed by the elements of arr2 +const arr = [1, ...arr2]; +``` + +Based on the description, it sounds easy to downlevel to ES5: + +```js +// The same, right? +const arr = [1].concat(arr2); +``` + +However, this is observably different in certain rare cases. +For example, if an array has a "hole" in it, the missing index will create an *own* property if spreaded, but will not if built using `concat`: + +```js +// Make an array where the '1' element is missing +let missing = [0, , 1]; +let spreaded = [...missing]; +let concated = [].concat(missing); + +// true +"1" in spreaded +// false +"1" in concated +``` + +Just as with `for / of`, `downlevelIteration` will use `Symbol.iterator` (if present) to more accurately emulate ES 6 behavior. diff --git a/packages/tsconfig-reference/copy/en/options/emitBOM.md b/packages/tsconfig-reference/copy/en/options/emitBOM.md index 533cd71fd19e..15090e5a3504 100644 --- a/packages/tsconfig-reference/copy/en/options/emitBOM.md +++ b/packages/tsconfig-reference/copy/en/options/emitBOM.md @@ -2,4 +2,6 @@ display: "Emit B O M" --- -Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. +Controls whether TypeScript will emit a [byte order mark (BOM)](https://en.wikipedia.org/wiki/Byte_order_mark) when writing output files. +Some runtime environments require a BOM to correctly interpret a JavaScript files; others require that it is not present. +The default value of `false` is generally best unless you have a reason to change it. diff --git a/packages/tsconfig-reference/copy/en/options/emitDeclarationOnly.md b/packages/tsconfig-reference/copy/en/options/emitDeclarationOnly.md index fe4e0a789a43..fe4086b2db24 100644 --- a/packages/tsconfig-reference/copy/en/options/emitDeclarationOnly.md +++ b/packages/tsconfig-reference/copy/en/options/emitDeclarationOnly.md @@ -2,4 +2,6 @@ display: "Emit Declaration Only" --- -Only emit '.d.ts' declaration files. +*Only* emit `.d.ts` files; do not emit `.js` files. + +This setting is useful if you're using a transpiler other than TypeScript to generate your JavaScript. diff --git a/packages/tsconfig-reference/copy/en/options/exclude.md b/packages/tsconfig-reference/copy/en/options/exclude.md new file mode 100644 index 000000000000..261002b0d5a1 --- /dev/null +++ b/packages/tsconfig-reference/copy/en/options/exclude.md @@ -0,0 +1,11 @@ +--- +display: "Exclude" +--- + +**Default**: `["node_modules", "bower_components", "jspm_packages"]`, plus the value of `outDir` if one is specified. + +Specifies an array of filenames or patterns that should be skipped when resolving `include`. + +**Important**: `exclude` *only* changes which files are included as a result of the `include` setting. +A file specified by `exclude` can still become part of your program due to an `import` statement in your code, a `types` inclusion, a `/// ❌ **Discouraged:** This flag was provided for backward compatibility reasons and should not be enabled in new or maintained codebases. + +This flag changes the `keyof` type operator to return `string` instead of `string | number` when applied to a type with a string index signature. + diff --git a/packages/tsconfig-reference/copy/en/options/lib.md b/packages/tsconfig-reference/copy/en/options/lib.md index a37f9e2dd5f5..ae45755426b2 100644 --- a/packages/tsconfig-reference/copy/en/options/lib.md +++ b/packages/tsconfig-reference/copy/en/options/lib.md @@ -2,4 +2,49 @@ display: "Lib" --- -Specify library files to be included in the compilation. +**Default**: At a minimum `["dom"]`, plus more depending on `target` + +TypeScript includes a default set of type definitions for built-in JS APIs (like `Math`), as well as type definitions for things found in browser environments (like `document`). +TypeScript also includes APIs for newer JS features matching the `target` you specify; for example the definition for `Map` is available if `target` is `ES6` or newer. + +You may want to change these for a few reasons: + * Your program doesn't run in a browser, so you don't want the `"dom"` type definitions + * Your runtime platform provides certain JavaScript API objects (maybe through polyfills), but doesn't yet support the full syntax of a given ECMAScript version + * You have polyfills or native implementations for some, but not all, of a higher level ECMAScript version + +| Name | Contents / Notes | +|-------------------------|----------------------------| +| ES5 | Core definitions for all ES3 and ES5 functionality | +| ES2015 | Additional APIs available in ES2015 (also known as ES6) | +| ES6 | Alias for "ES2015" | +| ES2016 | Additional APIs available in ES2016 | +| ES7 | Alias for "ES2016" | +| ES2017 | Additional APIs available in ES2017 | +| ES2018 | Additional APIs available in ES2017 | +| ESNext | Additional APIs available in ESNext | +| DOM | DOM definitions (`window`, `document`, etc.) | +| DOM.Iterable | | +| WebWorker | APIs available in WebWorker contexts | +| ScriptHost | | +| ES2015.Core | | +| ES2015.Collection | | +| ES2015.Generator | | +| ES2015.Iterable | | +| ES2015.Promise | | +| ES2015.Proxy | | +| ES2015.Reflect | | +| ES2015.Symbol | | +| ES2015.Symbol.WellKnown | | +| ES2016.Array.Include | | +| ES2017.object | | +| ES2017.Intl | | +| ES2017.SharedMemory | | +| ES2017.String | | +| ES2017.TypedArrays | | +| ES2018.Intl | | +| ES2018.Promise | | +| ES2018.RegExp | | +| ESNext.AsyncIterable | | +| ESNext.Array | | +| ESNext.Intl | | +| ESNext.Symbol | | diff --git a/packages/tsconfig-reference/copy/en/options/module.md b/packages/tsconfig-reference/copy/en/options/module.md index 3bfd658079ca..665925eb56e2 100644 --- a/packages/tsconfig-reference/copy/en/options/module.md +++ b/packages/tsconfig-reference/copy/en/options/module.md @@ -2,4 +2,6 @@ display: "Module" --- -Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. +**Allowed Values**: `CommonJS` (default if `target` is `ES3` or `ES5`), `ES6`/`ES2015` (synonymous, default for `target` `ES6` and higher), `None`, `UMD`, `AMD`, `System`, `ESNext` + +Sets the module system for the program. See the [[Modules]] chapter for more information. diff --git a/packages/tsconfig-reference/copy/en/options/noImplicitAny.md b/packages/tsconfig-reference/copy/en/options/noImplicitAny.md index 1207bbefd4a1..683f0004521c 100644 --- a/packages/tsconfig-reference/copy/en/options/noImplicitAny.md +++ b/packages/tsconfig-reference/copy/en/options/noImplicitAny.md @@ -2,4 +2,25 @@ display: "No Implicit Any" --- -Raise error on expressions and declarations with an implied 'any' type. +> ✅ We strongly recommend enabling this in all codebases + +**Default**: `false`, unless `strict` is set + +In some cases where no type annotations are present, TypeScript will fall back to a type of `any` for a variable. +This can cause some errors to be missed: + +```ts +// @noImplicitAny: false +function fn(s) { + // No error? + console.log(s.subtr(3)) +} +fn(42); +``` + +When `noImplicitAny` is set, TypeScript will issue an error whenever it would have inferred `any`: +```ts +function fn(s) { + console.log(s.subtr(3)) +} +``` diff --git a/packages/tsconfig-reference/copy/en/options/noImplicitUseStrict.md b/packages/tsconfig-reference/copy/en/options/noImplicitUseStrict.md index 11ee0128d3c1..e4fa08a97ab5 100644 --- a/packages/tsconfig-reference/copy/en/options/noImplicitUseStrict.md +++ b/packages/tsconfig-reference/copy/en/options/noImplicitUseStrict.md @@ -2,4 +2,22 @@ display: "No Implicit Use Strict" --- -Do not emit 'use strict' directives in module output. +> 🧙‍ You shouldn't need this + +By default, when emitting a module file to a non-ES6 target, TypeScript emits a `"use strict";` prologue at the top of the file. +This setting disables that. + +```ts +// @showEmit +// @target: ES3 +// @module: AMD +// @noImplicitUseStrict +export function fn() { } +``` + +```ts +// @showEmit +// @target: ES3 +// @module: AMD +export function fn() { } +``` diff --git a/packages/tsconfig-reference/copy/en/options/noLib.md b/packages/tsconfig-reference/copy/en/options/noLib.md index 97f5a952c659..384e7ec95cbb 100644 --- a/packages/tsconfig-reference/copy/en/options/noLib.md +++ b/packages/tsconfig-reference/copy/en/options/noLib.md @@ -2,4 +2,5 @@ display: "No Lib" --- -Do not include the default library file (lib.d.ts). +Disables the automatic inclusion of any library files. +If this option is set, `lib` is ignored. diff --git a/packages/tsconfig-reference/copy/en/options/noResolve.md b/packages/tsconfig-reference/copy/en/options/noResolve.md index b179553a3ecb..8ccac5bd125b 100644 --- a/packages/tsconfig-reference/copy/en/options/noResolve.md +++ b/packages/tsconfig-reference/copy/en/options/noResolve.md @@ -2,4 +2,9 @@ display: "No Resolve" --- -Do not add triple-slash references or imported modules to the list of compiled files. +> 🧙 This option is rarely used. + +By default, TypeScript will examine the initial set of files for `import` and `> ❌ **Deprecated:** Do not use this. Use [[outFile]] instead + +The `out` option computes the final file location in a way that is not predictable or consistent. +This option is retained for backward compatibility only. diff --git a/packages/tsconfig-reference/copy/en/options/outDir.md b/packages/tsconfig-reference/copy/en/options/outDir.md index 33608b5f9fab..0526e07b7136 100644 --- a/packages/tsconfig-reference/copy/en/options/outDir.md +++ b/packages/tsconfig-reference/copy/en/options/outDir.md @@ -2,4 +2,8 @@ display: "Out Dir" --- -Redirect output structure to the directory. +If specified, `.js` (as well as `.d.ts`, `.js.map`, etc.) files will be emitted in the specified directory. +The directory structure of the original source files is preserved; see [[rootDir]] if the computed root is not what you intended. + +If not specified, `.js` files will be emitted in the same directory as the `.ts` files they were generated from. + diff --git a/packages/tsconfig-reference/copy/en/options/outFile.md b/packages/tsconfig-reference/copy/en/options/outFile.md index b8e2f63b42f7..519353c63fbf 100644 --- a/packages/tsconfig-reference/copy/en/options/outFile.md +++ b/packages/tsconfig-reference/copy/en/options/outFile.md @@ -2,4 +2,9 @@ display: "Out File" --- -Concatenate and emit output to single file. +If specified, all *global* (non-module) files will be concatenated into the single output file specified. + +If `module` is `system` or `amd`, all module files will also be concatenated into this file after all global content. + +Note: `outFile` cannot be used unless `module` is `None`, `System`, or `AMD`. +This option *cannot* be used to bundle CommonJS or ES6 modules. diff --git a/packages/tsconfig-reference/copy/en/options/references.md b/packages/tsconfig-reference/copy/en/options/references.md new file mode 100644 index 000000000000..7f78b6fe02d4 --- /dev/null +++ b/packages/tsconfig-reference/copy/en/options/references.md @@ -0,0 +1,5 @@ +--- +display: "References" +--- + +TODO diff --git a/packages/tsconfig-reference/copy/en/options/rootDir.md b/packages/tsconfig-reference/copy/en/options/rootDir.md index 4b65870b0742..5c8d92a2172e 100644 --- a/packages/tsconfig-reference/copy/en/options/rootDir.md +++ b/packages/tsconfig-reference/copy/en/options/rootDir.md @@ -2,4 +2,64 @@ display: "Root Dir" --- -Specify the root directory of input files. Use to control the output directory structure with --outDir. +**Default**: The longest common path of all non-declaration input files. If `composite` is set, the default is instead the directory containing the `tsconfig.json` file. + +When TypeScript compiles files, it keeps the same directory structure in the output directory as exists in the input directory. + +For example, let's say you have some input files: + +``` +MyProj +├── tsconfig.json +├── core +│ ├── a.ts +│ ├── b.ts +│ ├── sub +│ │ ├── c.ts +├── types.d.ts +``` + +The inferred value for `rootDir` is the longest common path of all non-declaration input files, which in this case is `core/`. + +If your `outDir` was `dist`, TypeScript would write this tree: + +``` +MyProj +├── dist +│ ├── a.ts +│ ├── b.ts +│ ├── sub +│ │ ├── c.ts +``` + +However, you may have intended for `core` to be part of the output directory structure. +By setting `rootDir: "."` in `tsconfig.json`, TypeScript would write this tree: + +``` +MyProj +├── dist +| ├── core +│ │ ├── a.ts +│ │ ├── b.ts +│ │ ├── sub +│ │ │ ├── c.ts +``` + +Importantly, `rootDir` **does not affect which files become part of the compilation**. +It has no interaction with the `include`, `exclude`, or `files` `tsconfig.json` settings. + +Note that TypeScript will never write an output file to a directory outside of `outDir`, and will never skip emitting a file. +For this reason, `rootDir` also enforces that all files which need to be emitted are underneath the `rootDir` path. + +For example, let's say you had this tree: + +``` +MyProj +├── tsconfig.json +├── core +│ ├── a.ts +│ ├── b.ts +├── helpers.ts +``` + +It would be an error to specify `rootDir` as `core` *and* `include` as `*` because it creates a file (`helpers.ts`) that would need to be emitted *outside* the `outDir` (i.e. `../helpers.js`). diff --git a/packages/tsconfig-reference/copy/en/options/sourceMap.md b/packages/tsconfig-reference/copy/en/options/sourceMap.md index 29d2fcc2f3a5..f622c14be735 100644 --- a/packages/tsconfig-reference/copy/en/options/sourceMap.md +++ b/packages/tsconfig-reference/copy/en/options/sourceMap.md @@ -2,4 +2,11 @@ display: "Source Map" --- -Generates corresponding '.map' file. +Enables the generation of [sourcemap files](https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Use_a_source_map). +These files allow debuggers and other tools to display the original TypeScript source code when actually working with the emitted JavaScript files. +Source map files are emitted as `.js.map` (or `.jsx.map`) files next to the corresponding `.js` output file. + +The `.js` files will in turn contain a sourcemap comment to indicate to tools where the files are: +```js +//# sourceMappingURL=main.js.map +``` diff --git a/packages/tsconfig-reference/copy/en/options/strict.md b/packages/tsconfig-reference/copy/en/options/strict.md index 5d9921956dd8..10ca54e4a8fc 100644 --- a/packages/tsconfig-reference/copy/en/options/strict.md +++ b/packages/tsconfig-reference/copy/en/options/strict.md @@ -2,4 +2,11 @@ display: "Strict" --- -Enable all strict type-checking options. +> ✅ We recommend enabling this in all codebases, especially new ones + +The `strict` flag enables a wide range of type checking behavior that results in stronger guarantees of program correctness. +Turning this on is equivalent to enabling all of the *strict mode family* options, which are outlined below. +You can then turn off individual strict mode family checks as needed. + +Future versions of TypeScript may introduce additional stricter checking under this flag, so upgrades of TypeScript might result in new type errors in your program. +When appropriate and possible, a corresponding flag will be added to disable that behavior. diff --git a/packages/tsconfig-reference/copy/en/options/strictBindCallApply.md b/packages/tsconfig-reference/copy/en/options/strictBindCallApply.md index 8424b326ed8b..0d8e642691fe 100644 --- a/packages/tsconfig-reference/copy/en/options/strictBindCallApply.md +++ b/packages/tsconfig-reference/copy/en/options/strictBindCallApply.md @@ -2,4 +2,33 @@ display: "Strict Bind Call Apply" --- -Enable strict 'bind', 'call', and 'apply' methods on functions. +**Default**: `false`, unless `strict` is set. + +When set, TypeScript will check that the built-in methods of functions `call`, `bind`, and `apply` are invoked with correct argument for the underlying function: + +```ts +// With strictBindCallApply on +function fn(x: string) { + return parseInt(x); +} + +const n1 = fn.call(undefined, "10"); + ^? + +const n2 = fn.call(undefined, false); +``` + +Otherwise, these functions accept any arguments and will return `any`: + +```ts +// @strictBindCallApply: false + +// With strictBindCallApply off +function fn(x: string) { + return parseInt(x); +} + +// Note: No error; return type is 'any' +const n = fn.call(undefined, false); + ^? +``` diff --git a/packages/tsconfig-reference/copy/en/options/strictFunctionTypes.md b/packages/tsconfig-reference/copy/en/options/strictFunctionTypes.md index b0404b38bdda..9f7fd270e2d1 100644 --- a/packages/tsconfig-reference/copy/en/options/strictFunctionTypes.md +++ b/packages/tsconfig-reference/copy/en/options/strictFunctionTypes.md @@ -2,4 +2,54 @@ display: "Strict Function Types" --- -Enable strict checking of function types. +> ✅ We strongly recommend enabling this in all codebases + +**Default**: `false`, unless `strict` is set + +When enabled, this flag causes functions parameters to be checked more correctly. + +Here's a basic example with `strictFunctionTypes` *off*: + +```ts +// @strictFunctionTypes: false +function fn(x: string) { + console.log("Hello, " + x.toLowerCase()); +} + +type StringOrNumberFunc = (ns: string | number) => void; + +// Unsafe assignment +let func: StringOrNumberFunc = fn; +// Unsafe call - will crash +func(10); +``` + +With `strictFunctionTypes` *on*, the error is correctly detected: +```ts +function fn(x: string) { + console.log("Hello, " + x.toLowerCase()); +} + +type StringOrNumberFunc = (ns: string | number) => void; + +// Unsafe assignment is prevented +let func: StringOrNumberFunc = fn; +``` + +During development of this feature, we discovered a large number of inherently unsafe class hierarchies, including some in the DOM. +Because of this, the setting only applies to functions written in *function* syntax, not to those in *method* syntax: +```ts +type Methodish = { + func(x: string | number): void; +}; +function fn(x: string) { + console.log("Hello, " + x.toLowerCase()); +} + +// Ultimately an unsafe assignment, but not detected +const m: Methodish = { + func: fn +}; +m.func(10); +``` + diff --git a/packages/tsconfig-reference/copy/en/options/strictNullChecks.md b/packages/tsconfig-reference/copy/en/options/strictNullChecks.md index 2fe0d4f22a61..3ffa2eeb2805 100644 --- a/packages/tsconfig-reference/copy/en/options/strictNullChecks.md +++ b/packages/tsconfig-reference/copy/en/options/strictNullChecks.md @@ -2,4 +2,9 @@ display: "Strict Null Checks" --- -Enable strict null checks. +**Default**: `false`, unless `strict` is set + +When `strictNullChecks` is `false`, `null` and `undefined` are considered to be valid values of all types. +This can lead to unexpected errors at runtime. + +When `strictNullChecks` is `true`, `null` and `undefined` have their own distinct types and you'll get a type error if you try to use them where a concrete value is expected. diff --git a/packages/tsconfig-reference/copy/en/options/strictPropertyInitialization.md b/packages/tsconfig-reference/copy/en/options/strictPropertyInitialization.md index 0f9de4f105f2..7e0a3b4ed378 100644 --- a/packages/tsconfig-reference/copy/en/options/strictPropertyInitialization.md +++ b/packages/tsconfig-reference/copy/en/options/strictPropertyInitialization.md @@ -2,4 +2,4 @@ display: "Strict Property Initialization" --- -Enable strict checking of property initialization in classes. +When set, it becomes an error to declare a class property without directly initializing it before the end of the constructor. diff --git a/packages/tsconfig-reference/copy/en/options/suppressExcessPropertyErrors.md b/packages/tsconfig-reference/copy/en/options/suppressExcessPropertyErrors.md index 439c85881827..2af6855f2a33 100644 --- a/packages/tsconfig-reference/copy/en/options/suppressExcessPropertyErrors.md +++ b/packages/tsconfig-reference/copy/en/options/suppressExcessPropertyErrors.md @@ -2,4 +2,11 @@ display: "Suppress Excess Property Errors" --- -Suppress excess property checks for object literals. +> ❌ **Discouraged:** This flag is provided for backward compatibility. Consider using `@ts-ignore` instead. + +This disables reporting of excess property errors, such as the one shown in the following example + +```ts +type Point = { x: number, y: number }; +const p: Point = { x: 1, y: 3, m: 10 }; +``` diff --git a/packages/tsconfig-reference/copy/en/options/suppressImplicitAnyIndexErrors.md b/packages/tsconfig-reference/copy/en/options/suppressImplicitAnyIndexErrors.md index f5c980c1fc6b..6250a8f00205 100644 --- a/packages/tsconfig-reference/copy/en/options/suppressImplicitAnyIndexErrors.md +++ b/packages/tsconfig-reference/copy/en/options/suppressImplicitAnyIndexErrors.md @@ -2,4 +2,11 @@ display: "Suppress Implicit Any Index Errors" --- -Suppress noImplicitAny errors for indexing objects lacking index signatures. +> ❌ **Discouraged:** This flag is provided for backward compatibility. Consider using `@ts-ignore` instead. + +This disables reporting of implicit `any` warnings when indexing into objects, such as shown in the following example + +```ts +const obj = { x: 10 }; +console.log(obj["foo"]); +``` diff --git a/packages/tsconfig-reference/copy/en/options/target.md b/packages/tsconfig-reference/copy/en/options/target.md index 0f9b764ec3fe..15d3a17cc9a4 100644 --- a/packages/tsconfig-reference/copy/en/options/target.md +++ b/packages/tsconfig-reference/copy/en/options/target.md @@ -2,4 +2,16 @@ display: "Target" --- -Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. +**Allowed Values**: `ES3` (default), `ES5`, `ES6`/`ES2015` (synonomous), `ES7`/`ES2016`, `ES2017`, `ES2018`, `ES2019`, `ESNext` + +All modern browsers support all ES6 features, so `ES6` is a good choice. +You might choose to set a lower target if your code is deployed to older environments, or a higher target if your code only runs on newer environments. + +The `target` setting changes which JS features are downleveled or left intact. +For example, an arrow function `() => this` will be turned into an equivalent `function` expression if `target` is ES5 or lower. + +`target` also changes the default value of [[`lib`]]. +You may "mix and match" `target` and `lib` settings as desired. + +The value `ESNext` refers to whatever the highest version TypeScript supports at the time is. +This setting should be used with caution, since it doesn't mean the same thing between TypeScript versions and can make upgrades less predictable. diff --git a/packages/tsconfig-reference/copy/en/options/typeAcquisition.md b/packages/tsconfig-reference/copy/en/options/typeAcquisition.md new file mode 100644 index 000000000000..428ee170a56b --- /dev/null +++ b/packages/tsconfig-reference/copy/en/options/typeAcquisition.md @@ -0,0 +1,5 @@ +--- +display: "Type Acquisition" +--- + +TODO diff --git a/packages/tsconfig-reference/scripts/generateJSON.ts b/packages/tsconfig-reference/scripts/generateJSON.ts index ef85791bbea3..b3abbcee6c2b 100644 --- a/packages/tsconfig-reference/scripts/generateJSON.ts +++ b/packages/tsconfig-reference/scripts/generateJSON.ts @@ -75,7 +75,67 @@ filteredOptions.forEach(option => { delete option.showInSimplifiedHelpView }) -writeJSON('tsconfigOpts.json', filteredOptions) +const topLevelTSConfigOptions = [ + { + "name": "files", + "type": "An array of strings", + "category": 0, + "description": { + "message": "Print names of files part of the compilation." + }, + "defaultValue": "false" + }, + { + "name": "include", + "type": "An array of strings", + "category": 0, + "description": { + "message": "Print names of files part of the compilation." + }, + "defaultValue": "false" + }, + { + "name": "exclude", + "type": "An array of strings", + "category": 0, + "description": { + "message": "Print names of files part of the compilation." + }, + "defaultValue": "false" + }, + { + "name": "extends", + "type": "string", + "category": 0, + "description": { + "message": "Print names of files part of the compilation." + }, + "defaultValue": "false" + }, + { + "name": "typeAcquisition", + "type": "string", + "category": 0, + "description": { + "message": "Print names of files part of the compilation." + }, + "defaultValue": "false" + }, + { + "name": "references", + "type": "string", + "category": 0, + "description": { + "message": "Print names of files part of the compilation." + }, + "defaultValue": "false" + }, + + +] + + +writeJSON('tsconfigOpts.json', [...topLevelTSConfigOptions, ...filteredOptions]) // Improve the typing for the rules writeString( @@ -85,6 +145,16 @@ writeString( const categoryMap = {} categories.forEach(c => (categoryMap[c.code] = c)) + +// Add custom categories, for custom compiler flags + +categoryMap["0"] = { + code: 0, + category: 3, + key: "Project_Files_0", + message: "Project File Management" +} + writeJSON('tsconfigCategories.json', categoryMap) // @ts-ignore - Print the defaults for a TS Config file diff --git a/packages/tsconfig-reference/scripts/generateMarkdown.ts b/packages/tsconfig-reference/scripts/generateMarkdown.ts index 256c72c07815..e26c7b7765d1 100644 --- a/packages/tsconfig-reference/scripts/generateMarkdown.ts +++ b/packages/tsconfig-reference/scripts/generateMarkdown.ts @@ -16,6 +16,7 @@ const options = require('../data/tsconfigOpts.json') as typeof import('../data/t const categories = require('../data/tsconfigCategories.json') as typeof import('../data/tsconfigCategories.json') const orderedCategories = [ + 'Project_Files_0', 'Basic_Options_6172', 'Strict_Type_Checking_Options_6173', 'Module_Resolution_Options_6174', @@ -29,7 +30,7 @@ const orderedCategories = [ // Makes sure all categories are accounted for in ^ assert.deepEqual(Object.keys(categories).sort(), orderedCategories.map(c => c.split('_').pop()).sort()) -const languages = readdirSync(join(__dirname, '..', 'copy')) +const languages = readdirSync(join(__dirname, '..', 'copy')).filter(f => !f.startsWith(".")) languages.forEach(lang => { const locale = join(__dirname, '..', 'copy', lang) @@ -40,7 +41,9 @@ languages.forEach(lang => { const getPathInLocale = (path: string) => { if (existsSync(join(locale, path))) return join(locale, path) if (existsSync(join(fallbackLocale, path))) return join(fallbackLocale, path) - throw new Error('Could not find a path for ' + path) + + const localeDesc = lang === "en" ? lang : `either ${lang} or English` + throw new Error('Could not find a path for ' + path + " in " + localeDesc) } // Make a JSON dump of the category anchors someone wrapping the markdown @@ -48,15 +51,15 @@ languages.forEach(lang => { orderedCategories.forEach(categoryID => { const category = Object.values(categories).find((c: any) => c.key === categoryID) - assert.ok(category) + assert.ok(category, "Could not find category for ID: " + categoryID) const categoryPath = getPathInLocale(join('categories', categoryID + '.md')) const categoryFile = readMarkdownFile(categoryPath) - assert.ok(categoryFile.data.display) // Must have a display title in the front-matter + assert.ok(categoryFile.data.display, "No display data for category: " + categoryID) // Must have a display title in the front-matter // Let the title change it's display but keep the same ID - const title = `#

${categoryFile.data.display}

` + const title = `

# ${categoryFile.data.display}

` markdownChunks.push(title) allCategories.push({ display: categoryFile.data.display, anchor: categoryID }) @@ -73,15 +76,16 @@ languages.forEach(lang => { assert.ok(optionFile.data.display, 'Could not get a display for option: ' + option.name + ' in ' + lang) // Let the title change it's display but keep the same ID - const title = `#

${optionFile.data.display}

` - + const titleLink = `#` + const title = `

${titleLink} ${optionFile.data.display} - ${option.name}

` + markdownChunks.push(title) markdownChunks.push(optionFile.content) // Make a markdown table of the important metadata const mdTableRows = [] as [string, string][] - mdTableRows.push(['Value', '`' + option.name + '`']) + // mdTableRows.push(['Value', '`' + option.name + '`']) if (option.deprecated) mdTableRows.push(['Status', 'Deprecated']) if (option.defaultValue) { @@ -96,8 +100,6 @@ languages.forEach(lang => { const table = ` | | |\n | ---- | --- | \n` + mdTableRows.map(r => `${r[0]} | ${r[1]}`).join('\n') markdownChunks.push(table) - - markdownChunks.push('---') }) }) diff --git a/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_0_phone.png b/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_0_phone.png index 866849000147..33bc8bb97744 100644 Binary files a/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_0_phone.png and b/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_0_phone.png differ diff --git a/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_1_tablet.png b/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_1_tablet.png index 7cabe56f3cc5..b7da8f73acf3 100644 Binary files a/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_1_tablet.png and b/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_1_tablet.png differ diff --git a/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_2_computer.png b/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_2_computer.png index 1db13555fe80..616ea9656942 100644 Binary files a/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_2_computer.png and b/packages/typescriptlang-org/_tests/backstop_data/bitmaps_reference/backstop_default_TSConfig_Example_0_viewport_2_computer.png differ diff --git a/packages/typescriptlang-org/package.json b/packages/typescriptlang-org/package.json index f949fd922f3b..b087f6209d02 100644 --- a/packages/typescriptlang-org/package.json +++ b/packages/typescriptlang-org/package.json @@ -7,7 +7,7 @@ "scripts": { "build": "gatsby build", "develop": "gatsby develop", - "update-snapshots": "yarn build; backstop test; backstop approve", + "update-snapshots": "backstop test; backstop approve", "start": "npm run develop", "serve": "gatsby serve", "test": "echo 'no tests'"