You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A module using CommonJS patterns uses `module.exports`to describe the exported values. For example, here is a module which exports a function and a numerical constant:
The TypeScript playground can show you the `.d.ts` equivalent for JavaScript code. You can [try it yourself here](/play?useJavaScript=true#code/GYVwdgxgLglg9mABAcwKZQIICcsEMCeAMqmMlABYAUuOAlIgN6IBQiiW6IWSNWAdABsSZcswC+zCAgDOURAFtcADwAq5GKUQBeRAEYATM2by4AExBC+qJQAc4WKNO2NWKdNjxFhFADSvFquqk4sxAA).
The `.d.ts` syntax intentionally looks like [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) syntax.
36
-
ES Modules was ratified by TC39 in 2015 as part of ES2015 (ES6), while it has been available via transpilers for a long time, however if you have a JavaScript codebase using ES Modules:
Note that using `export default` in your .d.ts files requires [`esModuleInterop: true`](/tsconfig#esModuleInterop) to work.
96
-
If you can't have `esModuleInterop: true` in your project, such as when you're submitting a PR to Definitely Typed, you'll have to use the `export=` syntax instead. This older syntax is harder to use but works everywhere.
97
-
Here's how the above example would have to be written using `export=`:
See [Module: Functions](/zh/docs/handbook/declaration-files/templates/module-function-d-ts.html) for details of how that works, and the [Modules reference](/zh/docs/handbook/modules.html) page.
@@ -181,26 +176,24 @@ export function getArrayMetadata<ArrType>(
181
176
):ArrayMetadata<ArrType>;
182
177
```
183
178
184
-
Now the type of the array propagates into the `ArrayMetadata`type.
179
+
现在数组的类型会传播到 `ArrayMetadata`类型中。
185
180
186
-
The types which are exported can then be re-used by consumers of the modules using either `import`or`import type` in TypeScript code or [JSDoc imports](/zh/docs/handbook/jsdoc-supported-types.html#import-types).
/*~ If this module exports functions, declare them like so.
239
+
/*~ 如果此模块导出函数,请这样声明。
248
240
*/
249
241
exportfunction myFunction(a:string):string;
250
242
exportfunction myOtherFunction(a:number):number;
251
243
252
-
/*~ You can declare types that are available via importing the module*/
244
+
/*~ 你可以声明通过导入模块可用的类型*/
253
245
exportinterfaceSomeType {
254
246
name:string;
255
247
length:number;
256
248
extras?:string[];
257
249
}
258
250
259
-
/*~ You can declare properties of the module using const, let, or var */
251
+
/*~ 你可以使用 const、let 或 var 声明模块的属性*/
260
252
exportconst myField:number;
261
253
```
262
254
263
-
### Library file layout
255
+
### 库文件布局
264
256
265
-
The layout of your declaration files should mirror the layout of the library.
257
+
你的声明文件的布局应该与库的布局相对应。
266
258
267
-
A library can consist of multiple modules, such as
259
+
一个库可以由多个模块组成,比如
268
260
269
261
```
270
262
myLib
@@ -275,7 +267,7 @@ myLib
275
267
+---- baz.js
276
268
```
277
269
278
-
These could be imported as
270
+
这些可以被导入为
279
271
280
272
```js
281
273
var a =require("myLib");
@@ -284,7 +276,7 @@ var c = require("myLib/bar");
284
276
var d =require("myLib/bar/baz");
285
277
```
286
278
287
-
Your declaration files should thus be
279
+
因此,你的声明文件应该是
288
280
289
281
```
290
282
@types/myLib
@@ -295,17 +287,17 @@ Your declaration files should thus be
295
287
+---- baz.d.ts
296
288
```
297
289
298
-
### Testing your types
290
+
### 测试你的类型
299
291
300
-
If you are planning on submitting these changes to DefinitelyTyped for everyone to also use, then we recommend you:
292
+
如果你计划将这些更改提交给 DefinitelyTyped,以供其他人使用,那么我们建议你:
301
293
302
-
> 1.Create a new folder in `node_modules/@types/[libname]`
303
-
> 2.Create an `index.d.ts` in that folder, and copy the example in
304
-
> 3.See where your usage of the module breaks, and start to fill out the index.d.ts
305
-
> 4.When you're happy, clone [DefinitelyTyped/DefinitelyTyped](https://github.com/DefinitelyTyped)and follow the instructions in the README.
0 commit comments