|
1 | | -# gen-typescript-declarations |
| 1 | +## 🚨 Moved to [`Polymer/tools/packages/gen-typescript-declarations`][1] 🚨 |
2 | 2 |
|
3 | | -[](https://travis-ci.org/Polymer/gen-typescript-declarations) |
4 | | -[](https://www.npmjs.com/package/@polymer/gen-typescript-declarations) |
| 3 | +The [`Polymer/gen-typescript-declarations`][2] repo has been migrated to [`packages/gen-typescript-declarations`][1] folder of the [`Polymer/tools`][3] 🚝 *monorepo*. |
5 | 4 |
|
6 | | -A library which generates TypeScript declarations for Polymer and custom |
7 | | -elements. |
| 5 | +We are *actively* working on migrating open Issues and PRs to the new repo. New Issues and PRs should be filed at [`Polymer/tools`][3]. |
8 | 6 |
|
9 | | -## How do I use the typings? |
10 | | - |
11 | | -### Polymer |
12 | | - |
13 | | -Typings for Polymer 2 are planned for regular release starting with version |
14 | | -2.4, and are available in the |
15 | | -[`types/`](https://github.com/Polymer/polymer/tree/master/types) directory on |
16 | | -the `master` branch now. |
17 | | - |
18 | | -Once Polymer 2.4 is released, to use the typings, install Polymer normally from |
19 | | -Bower, and add a [triple-slash |
20 | | -directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) |
21 | | -anywhere in your TypeScript project for the typings you require. Each HTML |
22 | | -import from Polymer has a corresponding typings file. For example, if you |
23 | | -depend on `polymer-element.html`: |
24 | | - |
25 | | -```ts |
26 | | -/// <reference path="./bower_components/polymer/types/polymer-element.d.ts" />` |
27 | | - |
28 | | -class MyElement extends Polymer.Element { |
29 | | - ... |
30 | | -} |
31 | | -``` |
32 | | - |
33 | | -Alternatively, you can add the dependency to `tsconfig.json` in the root of your project: |
34 | | - |
35 | | -```javascript |
36 | | -{ |
37 | | -... |
38 | | - "include": [ |
39 | | - "src/**/*.ts", |
40 | | - "src/bower_components/polymer/**/*.d.ts", |
41 | | - ] |
42 | | -} |
43 | | -``` |
44 | | - |
45 | | -Typings for Polymer 3 are planned but not yet available (see |
46 | | -[#18](https://github.com/Polymer/gen-typescript-declarations/issues/18)). |
47 | | - |
48 | | -You may also be interested in the [Polymer |
49 | | -decorators](https://github.com/Polymer/polymer-decorators). |
50 | | - |
51 | | -## How do I generate new typings? |
52 | | - |
53 | | -You can run this package from the command line with `gen-tsd`, or as a library |
54 | | -with the `generateDeclarations` function. |
55 | | - |
56 | | -It is recommended to integrate typings generation as part of your build/release |
57 | | -process: |
58 | | - |
59 | | -```sh |
60 | | -$ npm install --save-dev @polymer/gen-typescript-declarations |
61 | | -``` |
62 | | - |
63 | | -Add a `gen-tsd` script to your `package.json`: |
64 | | - |
65 | | -```js |
66 | | -{ |
67 | | - ... |
68 | | - "scripts": { |
69 | | - "gen-tsd": "gen-tsd" |
70 | | - } |
71 | | -} |
72 | | -``` |
73 | | - |
74 | | -If you're using Bower, ensure you run `npm run gen-tsd` to generate the latest |
75 | | -typings and commit them to your repository before tagging each release. |
76 | | - |
77 | | -If you're using NPM, you can instead add this script to the NPM `prepack` |
78 | | -script to generate and include typings in your NPM package every time you |
79 | | -publish. |
80 | | - |
81 | | -## Config options |
82 | | - |
83 | | -By default the `gen-tsd` command will read a file called `gen-tsd.json` in |
84 | | -your root directory. It has the following options: |
85 | | - |
86 | | -* **`excludeFiles`**`: string[]` |
87 | | - |
88 | | - Skip source files whose paths match any of these glob patterns. If |
89 | | - `undefined`, defaults to excluding directories ending in "test" or "demo". |
90 | | - |
91 | | -* **`excludeIdentifiers`**`: string[]` |
92 | | - |
93 | | - Do not emit any declarations for features that have any of these identifiers. |
94 | | - |
95 | | -* **`removeReferences`**`: string[]` |
96 | | - |
97 | | - Remove any triple-slash references to these files, specified as paths |
98 | | - relative to the analysis root directory. |
99 | | - |
100 | | -* **`addReferences`**`: {[filepath: string]: string[]}` |
101 | | - |
102 | | - Additional files to insert as triple-slash reference statements. Given the |
103 | | - map `a: b[]`, a will get an additional reference statement for each file |
104 | | - path in b. All paths are relative to the analysis root directory. |
105 | | - |
106 | | -* **`renameTypes`**`: {[name: string]: string}` |
107 | | - |
108 | | - Whenever a type with a name in this map is encountered, replace it with |
109 | | - the given name. Note this only applies to named types found in places like |
110 | | - function/method parameters and return types. It does not currently rename |
111 | | - e.g. entire generated classes. |
112 | | - |
113 | | -## Using as a module |
114 | | - |
115 | | -You can also use this package as a module: |
116 | | - |
117 | | -```js |
118 | | -import {generateDeclarations} from 'gen-typescript-declarations'; |
119 | | - |
120 | | -const config = { |
121 | | - "exclude": [ |
122 | | - "test/**", |
123 | | - ], |
124 | | - "removeReferences": [ |
125 | | - "../shadycss/apply-shim.d.ts", |
126 | | - ], |
127 | | - "addReferences": { |
128 | | - "lib/utils/boot.d.ts": [ |
129 | | - "extra-types.d.ts" |
130 | | - ] |
131 | | - }, |
132 | | - "renameTypes": { |
133 | | - "Polymer_PropertyEffects": "Polymer.PropertyEffects" |
134 | | - } |
135 | | -} |
136 | | - |
137 | | -// A map of d.ts file paths to file contents. |
138 | | -const declarations = await generateDeclarations('/my/root/dir', config); |
139 | | -``` |
140 | | - |
141 | | -## FAQ |
142 | | - |
143 | | -### Why are some typings missing? |
144 | | -This library is based on [Polymer |
145 | | -Analyzer](https://github.com/Polymer/polymer-analyzer) which has limitations in |
146 | | -its static analysis. For certain patterns, Analyzer relies on additional JSDoc |
147 | | -annotations. |
148 | | - |
149 | | -### Can I augment the generated typings with hand-written ones? |
150 | | -Yes, see the `addReferences` config option above. |
| 7 | +[1]: https://github.com/Polymer/tools/tree/master/packages/gen-typescript-declarations |
| 8 | +[2]: https://github.com/Polymer/gen-typescript-declarations |
| 9 | +[3]: https://github.com/Polymer/tools |
0 commit comments