Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -95,7 +95,7 @@ If your type definitions depend on another package:

When TypeScript opens a `package.json` file to figure out which files it needs to read, it first looks at a field called `typesVersions`.

#### Folder redirects (using `*`)
### Folder redirects (using `*`)

A `package.json` with a `typesVersions` field might look like this:

Expand All @@ -121,7 +121,7 @@ If we import from `package-name/foo`, we'll try to look for `[...]/node_modules/
What if we're not running in TypeScript 3.1 in this example?
Well, if none of the fields in `typesVersions` get matched, TypeScript falls back to the `types` field, so here TypeScript 3.0 and earlier will be redirected to `[...]/node_modules/package-name/index.d.ts`.

#### File redirects
### File redirects

When you want to only change the resolution for a single file at a time, you can tell TypeScript the file to resolve differently by passing in the exact filenames:

Expand Down
4 changes: 2 additions & 2 deletions packages/documentation/copy/en/handbook-v2/Everyday Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ Enums are a feature added to JavaScript by TypeScript which allows for describin
It's worth mentioning the rest of the primitives in JavaScript which are represented in the type system.
Though we will not go into depth here.

#### `bigint`
### `bigint`

From ES2020 onwards, there is a primitive in JavaScript used for very large integers, `BigInt`:

Expand All @@ -720,7 +720,7 @@ const anotherHundred: bigint = 100n;

You can learn more about BigInt in [the TypeScript 3.2 release notes](/docs/handbook/release-notes/typescript-3-2.html#bigint).

#### `symbol`
### `symbol`

There is a primitive in JavaScript used to create a globally unique reference via the function `Symbol()`:

Expand Down
12 changes: 6 additions & 6 deletions packages/documentation/copy/en/handbook-v2/Modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ type Animals = Cat | Dog;

TypeScript has extended the `import` syntax with two concepts for declaring an import of a type:

###### `import type`
##### `import type`

Which is an import statement which can _only_ import types:

Expand All @@ -221,7 +221,7 @@ import type { createCatName } from "./animal.js";
const name = createCatName();
```

###### Inline `type` imports
##### Inline `type` imports

TypeScript 4.5 also allows for individual imports to be prefixed with `type` to indicate that the imported reference is a type:

Expand Down Expand Up @@ -258,7 +258,7 @@ You can learn more about this syntax in the [modules reference page](/docs/handb

CommonJS is the format which most modules on npm are delivered in. Even if you are writing using the ES Modules syntax above, having a brief understanding of how CommonJS syntax works will help you debug easier.

#### Exporting
### Exporting

Identifiers are exported via setting the `exports` property on a global called `module`.

Expand Down Expand Up @@ -365,7 +365,7 @@ import { valueOfPi } from "./constants.js";
export const twoPi = valueOfPi * 2;
```

#### `ES2020`
### `ES2020`

```ts twoslash
// @showEmit
Expand All @@ -376,7 +376,7 @@ import { valueOfPi } from "./constants.js";
export const twoPi = valueOfPi * 2;
```

#### `CommonJS`
### `CommonJS`

```ts twoslash
// @showEmit
Expand All @@ -387,7 +387,7 @@ import { valueOfPi } from "./constants.js";
export const twoPi = valueOfPi * 2;
```

#### `UMD`
### `UMD`

```ts twoslash
// @showEmit
Expand Down
6 changes: 3 additions & 3 deletions packages/documentation/copy/en/handbook-v2/Narrowing.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ In addition, classes can [use `this is Type`](/docs/handbook/2/classes.html#this

Types can also be narrowed using [Assertion functions](/docs/handbook/release-notes/typescript-3-7.html#assertion-functions).

# Discriminated unions
## Discriminated unions

Most of the examples we've looked at so far have focused around narrowing single variables with simple types like `string`, `boolean`, and `number`.
While this is common, most of the time in JavaScript we'll be dealing with slightly more complex structures.
Expand Down Expand Up @@ -701,12 +701,12 @@ From there, the type system was able to do the "right" thing and figure out the
Discriminated unions are useful for more than just talking about circles and squares.
They're good for representing any sort of messaging scheme in JavaScript, like when sending messages over the network (client/server communication), or encoding mutations in a state management framework.

# The `never` type
## The `never` type

When narrowing, you can reduce the options of a union to a point where you have removed all possibilities and have nothing left.
In those cases, TypeScript will use a `never` type to represent a state which shouldn't exist.

# Exhaustiveness checking
## Exhaustiveness checking

The `never` type is assignable to every type; however, no type is assignable to `never` (except `never` itself). This means you can use narrowing and rely on `never` turning up to do exhaustive checking in a `switch` statement.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ let c = createLabel(Math.random() ? "hello" : 42);
// ^?
```

### Conditional Type Constraints
## Conditional Type Constraints

Often, the checks in a conditional type will provide us with some new information.
Just like narrowing with type guards can give us a more specific type, the true branch of a conditional type will further constrain generics by the type we check against.
Expand Down Expand Up @@ -172,7 +172,7 @@ type Num = Flatten<number>;
When `Flatten` is given an array type, it uses an indexed access with `number` to fetch out `string[]`'s element type.
Otherwise, it just returns the type it was given.

### Inferring Within Conditional Types
## Inferring Within Conditional Types

We just found ourselves using conditional types to apply constraints and then extract out types.
This ends up being such a common operation that conditional types make it easier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type FeatureOptions = OptionsFlags<Features>;
// ^?
```

### Mapping Modifiers
## Mapping Modifiers

There are two additional modifiers which can be applied during mapping: `readonly` and `?` which affect mutability and optionality respectively.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

We generally recommend that people use ahead-of-time generation for large string unions, but this is useful in smaller cases.

### String Unions in Types
## String Unions in Types

The power in template literals comes when defining a new string based on information inside a type.

Expand Down Expand Up @@ -124,7 +124,7 @@ person.on("firstName", () => {});
person.on("frstNameChanged", () => {});
```

### Inference with Template Literals
## Inference with Template Literals

Notice that we did not benefit from all the information provided in the original passed object. Given change of a `firstName` (i.e. a `firstNameChanged` event), we should expect that the callback will receive an argument of type `string`. Similarly, the callback for a change to `age` should receive a `number` argument. We're naively using `any` to type the `callback`'s argument. Again, template literal types make it possible to ensure an attribute's data type will be the same type as that attribute's callback's first argument.

Expand Down Expand Up @@ -181,7 +181,7 @@ To help with string manipulation, TypeScript includes a set of types which can b

Converts each character in the string to the uppercase version.

##### Example
#### Example

```ts twoslash
type Greeting = "Hello, world"
Expand All @@ -197,7 +197,7 @@ type MainID = ASCIICacheKey<"my_app">

Converts each character in the string to the lowercase equivalent.

##### Example
#### Example

```ts twoslash
type Greeting = "Hello, world"
Expand All @@ -213,7 +213,7 @@ type MainID = ASCIICacheKey<"MY_APP">

Converts the first character in the string to an uppercase equivalent.

##### Example
#### Example

```ts twoslash
type LowercaseGreeting = "hello, world";
Expand All @@ -225,7 +225,7 @@ type Greeting = Capitalize<LowercaseGreeting>;

Converts the first character in the string to a lowercase equivalent.

##### Example
#### Example

```ts twoslash
type UppercaseGreeting = "HELLO WORLD";
Expand Down
8 changes: 4 additions & 4 deletions packages/documentation/copy/en/javascript/JSDoc Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Note:
- Any tags which are not explicitly listed below (such as `@async`) are not yet supported.
- Only documentation tags are supported in TypeScript files. The rest of the tags are only supported in JavaScript files.

#### Types
## Types

- [`@type`](#type)
- [`@import`](#import)
Expand All @@ -25,7 +25,7 @@ Note:
- [`@satisfies`](#satisfies)


#### Classes
## Classes

- [Property Modifiers](#property-modifiers) `@public`, `@private`, `@protected`, `@readonly`
- [`@override`](#override)
Expand All @@ -34,15 +34,15 @@ Note:
- [`@class`](#constructor) (or [`@constructor`](#constructor))
- [`@this`](#this)

#### Documentation
## Documentation

Documentation tags work in both TypeScript and JavaScript.

- [`@deprecated`](#deprecated)
- [`@see`](#see)
- [`@link`](#link)

#### Other
## Other

- [`@enum`](#enum)
- [`@author`](#author)
Expand Down
4 changes: 2 additions & 2 deletions packages/documentation/copy/en/reference/Mixins.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ This pattern relies less on the compiler, and more on your codebase to ensure bo
The mixin pattern is supported natively inside the TypeScript compiler by code flow analysis.
There are a few cases where you can hit the edges of the native support.

#### Decorators and Mixins [`#4881`](https://github.com/microsoft/TypeScript/issues/4881)
### Decorators and Mixins [`#4881`](https://github.com/microsoft/TypeScript/issues/4881)

You cannot use decorators to provide mixins via code flow analysis:

Expand Down Expand Up @@ -245,7 +245,7 @@ const playerTwo = (new Player() as unknown) as FreezablePlayer;
playerTwo.shouldFreeze;
```

#### Static Property Mixins [`#17829`](https://github.com/microsoft/TypeScript/issues/17829)
### Static Property Mixins [`#17829`](https://github.com/microsoft/TypeScript/issues/17829)

More of a gotcha than a constraint.
The class expression pattern creates singletons, so they can't be mapped at the type system to support different variable types.
Expand Down
12 changes: 6 additions & 6 deletions packages/documentation/copy/en/reference/Namespaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Even though the files are separate, they can each contribute to the same namespa
Because there are dependencies between files, we'll add reference tags to tell the compiler about the relationships between the files.
Our test code is otherwise unchanged.

##### Validation.ts
### Validation.ts

```ts
namespace Validation {
Expand All @@ -135,7 +135,7 @@ namespace Validation {
}
```

##### LettersOnlyValidator.ts
### LettersOnlyValidator.ts

```ts
/// <reference path="Validation.ts" />
Expand All @@ -149,7 +149,7 @@ namespace Validation {
}
```

##### ZipCodeValidator.ts
### ZipCodeValidator.ts

```ts
/// <reference path="Validation.ts" />
Expand All @@ -163,7 +163,7 @@ namespace Validation {
}
```

##### Test.ts
### Test.ts

```ts
/// <reference path="Validation.ts" />
Expand Down Expand Up @@ -208,7 +208,7 @@ tsc --outFile sample.js Validation.ts LettersOnlyValidator.ts ZipCodeValidator.t
Alternatively, we can use per-file compilation (the default) to emit one JavaScript file for each input file.
If multiple JS files get produced, we'll need to use `<script>` tags on our webpage to load each emitted file in the appropriate order, for example:

##### MyTestPage.html (excerpt)
### MyTestPage.html (excerpt)

```html
<script src="Validation.js" type="text/javascript" />
Expand Down Expand Up @@ -256,7 +256,7 @@ Because this library is loaded through a `<script>` tag (instead of a module loa
For the TypeScript compiler to see this shape, we use an ambient namespace declaration.
For example, we could begin writing it as follows:

##### D3.d.ts (simplified excerpt)
### D3.d.ts (simplified excerpt)

```ts
declare namespace D3 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ This can lead to problems when other tools are used to process the resulting mod

The `amd-module` directive allows passing an optional module name to the compiler:

##### amdModule.ts
### amdModule.ts

```ts
/// <amd-module name="NamedModule"/>
Expand All @@ -102,7 +102,7 @@ export class C {}

Will result in assigning the name `NamedModule` to the module as part of calling the AMD `define`:

##### amdModule.js
### amdModule.js

```js
define("NamedModule", ["require", "exports"], function (require, exports) {
Expand Down
Loading
Loading