From 86f15ecde8ce8ac3042c6be0abcc49e2fea9d859 Mon Sep 17 00:00:00 2001 From: Basarat Ali Syed Date: Sat, 8 Oct 2016 16:26:04 +1100 Subject: [PATCH] rudimentary type docs. Closes https://github.com/basarat/typescript-book/issues/148 --- SUMMARY.md | 1 + docs/types/@types.md | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 docs/types/@types.md diff --git a/SUMMARY.md b/SUMMARY.md index 79ee5df69..4e2496476 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -36,6 +36,7 @@ * [Browser QuickStart](docs/quick/browser.md) * [TypeScript's Type System](docs/types/type-system.md) * [JS Migration Guide](docs/types/migrating.md) + * [@types](docs/types/@types.md) * [Ambient Declarations](docs/types/ambient/intro.md) * [Declaration Files](docs/types/ambient/d.ts.md) * [Variables](docs/types/ambient/variables.md) diff --git a/docs/types/@types.md b/docs/types/@types.md new file mode 100644 index 000000000..0b616fc93 --- /dev/null +++ b/docs/types/@types.md @@ -0,0 +1,46 @@ +# `@types` + +[Definitely Typed](https://github.com/DefinitelyTyped/DefinitelyTyped) is definitely one of TypeScript's greatest strengths. The community has effectively gone ahead and **documented** the nature of nearly 90% of the top JavaScript projects out there. + +This means that you can use these projects in a very interactive an exploratory manner, no need to have the docs open in a seperate window and making sure you don't make a typo. + +## Using `@types` + +Installation is fairly simple as it just works on top of `npm`. So as an example you can install type definitions for `jquery` simply as : + +``` +npm install @types/jquery --save-dev +``` + +`@types` supports both *global* and *module* type definitions. + + +### Global `@types` + +After installation, to use a type definition globally you simply add it to your tsconfig.json e.g. to use jquery + +``` +{ + "compilerOptions": { + "types": [ + "jquery" + ] + } +} +``` + +Now you should be able to use `$` or `jQuery` *globally* in your project. + +> Alternatively, if you don't include it in tsconfig.json, you can use it only in particular files simply by adding `/// ` to the top of that file. (I personally don't do this). + +For *libraries* I generally recommend using *modules*: + +### Module `@types` + +After installation, no special configuration required really. You just use it like a module e.g. + +``` +import * as $ from "jquery"; + +// Use $ at will in this module :) +```