Skip to content

Commit 9e91f2f

Browse files
author
mk360
committed
start type declarations, prepare handbook
1 parent 2a9dcc3 commit 9e91f2f

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: The TypeScript Handbook
3+
layout: docs
4+
permalink: /docs/handbook/intro.html
5+
oneline: Your first step to learn TypeScript
6+
handbook: "true"
7+
---
8+
9+
## About this Handbook
10+
11+
Over 20 years after its introduction to the programming community, JavaScript is now one of the most widespread cross-platform languages ever created. Starting as a small scripting language for adding trivial interactivity to webpages, JavaScript has grown to be a language of choice for both frontend and backend applications of every size. While the size, scope, and complexity of programs written in JavaScript has grown exponentially, the ability of the JavaScript language to express the relationships between different units of code has not. Combined with JavaScript's rather peculiar runtime semantics, this mismatch between language and program complexity has made JavaScript development a difficult task to manage at scale.
12+
13+
The most common kinds of errors that programmers write can be described as type errors: a certain kind of value was used where a different kind of value was expected. This could be due to simple typos, a failure to understand the API surface of a library, incorrect assumptions about runtime behavior, or other errors. The goal of TypeScript is to be a static typechecker for JavaScript programs - in other words, a tool that runs before your code runs (static) and ensures that the types of the program are correct (typechecked).
14+
15+
If you are coming to TypeScript without a JavaScript background, with the intention of TypeScript being your first language, we recommend you first start reading the documentation on either the [Microsoft Learn JavaScript tutorial](https://docs.microsoft.com/javascript/) or read [JavaScript at the Mozilla Web Docs](https://developer.mozilla.org/docs/Web/JavaScript/Guide).
16+
If you have experience in other languages, you should be able to pick up JavaScript syntax quite quickly by reading the handbook.
17+
18+
## How is this Handbook Structured
19+
20+
The handbook is split into two sections:
21+
22+
- **The Handbook**
23+
24+
The TypeScript Handbook is intended to be a comprehensive document that explains TypeScript to everyday programmers. You can read the handbook by going from top to bottom in the left-hand navigation.
25+
26+
You should expect each chapter or page to provide you with a strong understanding of the given concepts. The TypeScript Handbook is not a complete language specification, but it is intended to be a comprehensive guide to all of the language's features and behaviors.
27+
28+
A reader who completes the walkthrough should be able to:
29+
30+
- Read and understand commonly-used TypeScript syntax and patterns
31+
- Explain the effects of important compiler options
32+
- Correctly predict type system behavior in most cases
33+
34+
In the interests of clarity and brevity, the main content of the Handbook will not explore every edge case or minutiae of the features being covered. You can find more details on particular concepts in the reference articles.
35+
36+
- **Reference Files**
37+
38+
The reference section below the handbook in the navigation is built to provide a richer understanding of how a particular part of TypeScript works. You can read it top-to-bottom, but each section aims to provide a deeper explanation of a single concept - meaning there is no aim for continuity.
39+
40+
### Non-Goals
41+
42+
The Handbook is also intended to be a concise document that can be comfortably read in a few hours. Certain topics won't be covered in order to keep things short.
43+
44+
Specifically, the Handbook does not fully introduce core JavaScript basics like functions, classes, and closures. Where appropriate, we'll include links to background reading that you can use to read up on those concepts.
45+
46+
The Handbook also isn't intended to be a replacement for a language specification. In some cases, edge cases or formal descriptions of behavior will be skipped in favor of high-level, easier-to-understand explanations. Instead, there are separate reference pages that more precisely and formally describe many aspects of TypeScript's behavior. The reference pages are not intended for readers unfamiliar with TypeScript, so they may use advanced terminology or reference topics you haven't read about yet.
47+
48+
Finally, the Handbook won't cover how TypeScript interacts with other tools, except where necessary. Topics like how to configure TypeScript with webpack, rollup, parcel, react, babel, closure, lerna, rush, bazel, preact, vue, angular, svelte, jquery, yarn, or npm are out of scope - you can find these resources elsewhere on the web.
49+
50+
## Get Started
51+
52+
Before getting started with [The Basics](/docs/handbook/2/basic-types.html), we recommend reading one of the following introductory pages. These introductions are intended to highlight key similarities and differences between TypeScript and your favored programming language, and clear up common misconceptions specific to those languages.
53+
54+
- [TypeScript for New Programmers](/docs/handbook/typescript-from-scratch.html)
55+
- [TypeScript for JavaScript Programmers](/docs/handbook/typescript-in-5-minutes.html)
56+
- [TypeScript for OOP Programmers](/docs/handbook/typescript-in-5-minutes-oop.html)
57+
- [TypeScript for Functional Programmers](/docs/handbook/typescript-in-5-minutes-func.html)
58+
59+
Otherwise, jump to [The Basics](/docs/handbook/2/basic-types.html) or grab a copy in [Epub](/assets/typescript-handbook.epub) or [PDF](/assets/typescript-handbook.pdf) form.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: Type Declarations
3+
layout: docs
4+
permalink: /fr/docs/handbook/2/type-declarations.html
5+
oneline: "Comment TypeScript fournit du typage pour du code JavaScript."
6+
---
7+
8+
Jusque-là, nous avons présenté des concepts basiques de TypeScript en utilisant des fonctionnalités présentes dans tous les moteurs JavaScript.
9+
De nos jours, JavaScript contient beaucoup de librairies qui accomplissent des tâches communes.
10+
Avoir du typage sur les parties d'application qui _ne font pas_ partie de votre code améliorera grandement votre expérience TypeScript.
11+
Where do these types come from?
12+
13+
## À quoi ressemblent les déclarations de types ?
14+
15+
Supposons que vous avez ce code :
16+
17+
```ts twoslash
18+
// @errors: 2339
19+
const k = Math.max(5, 6);
20+
const j = Math.mix(7, 8);
21+
```
22+
23+
Comment TypeScript a-t-il su que `max` existe, mais pas `mix`, même sans que l'implémentation de `Math` fasse partie de votre code ?
24+
25+
Il existe des _fichiers de déclarations_ qui décrivent ces objets pré-existants.
26+
Un fichier de déclarations fournit une manière de _déclarer_ l'existence de certains types ou certaines valeurs, sans leur fournir d'implémentation.
27+
28+
## Fichiers `.d.ts`
29+
30+
TypeScript a deux types principaux de fichiers.
31+
Les fichiers `.ts` sont des fichiers d'_implémentation_, qui contiennent du code exécutable.
32+
Ce sont les fichiers qui émettent des fichiers `.js` en sortie. C'est là que vous écrirez votre code.
33+
34+
Les fichiers `.d.ts` sont des fichiers de _déclarations_, qui ne contiennent _que_ des informations de types.
35+
Ces fichiers n'émettent pas de sortie `.js`; ils servent uniquement à la vérification de types.
36+
Nous apprendrons comment écrire nos propres fichiers de déclarations plus tard.
37+
38+
## Définitions de Types Pré-existantes
39+
40+
TypeScript possède des fichiers de déclarations pour toutes les APIs standard pré-existantes dans les moteurs JavaScript.
41+
Ils contiennent, par exemple, les propriétés des types `string` ou `function`, les objets globaux comme `Math` et `Object`, ainsi que leurs types associés.
42+
Par défaut, TypeScript contient également les types des éléments du navigateur, comme `window` et `document`; que l'on appelle collectivement les APIs de DOM (_DOM APIs_).
43+
44+
Ces fichiers suivent la convention de nommage `lib.[quelque chose].d.ts`.
45+
Si vous ouvrez un fichier suivant ce nommage, vous saurez que c'est un élément pré-existant de la plateforme, qui ne contient pas de code.
46+
47+
### Option `target`
48+
49+
Les méthodes, propriétés, et fonctions disponibles varient, dans les faits, en fonction de la _version_ de JavaScript que votre code exécute.
50+
Par exemple, la méthode `startsWith` des chaînes de caractères est disponible à partir de la 6ème version de JavaScript (_EcmaScript 6_).
51+
52+
Il est important de connaître quelle version de JavaScript est exécutée, car vous ne voulez pas utiliser d'APIs disponibles sur des versions plus récentes.
53+
C'est l'un des rôles de l'option de compilateur [`target`](/tsconfig#target).
54+
55+
TypeScript helps with this problem by varying which `lib` files are included by default based on your [`target`](/tsconfig#target) setting.
56+
For example, if [`target`](/tsconfig#target) is `ES5`, you will see an error if trying to use the `startsWith` method, because that method is only available in `ES6` or later.
57+
58+
### `lib` setting
59+
60+
The [`lib`](/tsconfig#lib) setting allows more fine-grained control of which built-in declaration files are considered available in your program.
61+
See the documentation page on [`lib`](/tsconfig#lib) for more information.
62+
63+
## External Definitions
64+
65+
For non-built-in APIs, there are a variety of ways you can get declaration files.
66+
How you do this depends on exactly which library you're getting types for.
67+
68+
### Bundled Types
69+
70+
If a library you're using is published as an npm package, it may include type declaration files as part of its distribution already.
71+
You can read the project's documentation to find out, or simply try importing the package and see if TypeScript is able to automatically resolve the types for you.
72+
73+
If you're a package author considering bundling type definitions with your package, you can read our guide on [bundling type definitions](/docs/handbook/declaration-files/publishing.html#including-declarations-in-your-npm-package).
74+
75+
### DefinitelyTyped / `@types`
76+
77+
The [DefinitelyTyped repository](https://github.com/DefinitelyTyped/DefinitelyTyped/) is a centralized repo storing declaration files for thousands of libraries.
78+
The vast majority of commonly-used libraries have declaration files available on DefinitelyTyped.
79+
80+
Definitions on DefinitelyTyped are also automatically published to npm under the `@types` scope.
81+
The name of the types package is always the same as the name of the underlying package itself.
82+
For example, if you installed the `react` npm package, you can install its corresponding types by running
83+
84+
```sh
85+
npm install --save-dev @types/react
86+
```
87+
88+
TypeScript automatically finds type definitions under `node_modules/@types`, so there's no other step needed to get these types available in your program.
89+
90+
### Your Own Definitions
91+
92+
In the uncommon event that a library didn't bundle its own types and didn't have a definition on DefinitelyTyped, you can write a declaration file yourself.
93+
See the appendix [Writing Declaration Files](/docs/handbook/declaration-files/introduction.html) for a guide.
94+
95+
If you want to silence warnings about a particular module without writing a declaration file, you can also quick declare the module as type `any` by putting an empty declaration for it in a `.d.ts` file in your project.
96+
For example, if you wanted to use a module named `some-untyped-module` without having definitions for it, you would write:
97+
98+
```ts twoslash
99+
declare module "some-untyped-module";
100+
```

0 commit comments

Comments
 (0)