Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init translation of module.class.d.ts #102

Merged
merged 1 commit into from
Nov 2, 2024
Merged
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
@@ -1,17 +1,17 @@
---
title: "Module: Class"
layout: docs
title: "模块:类"
layout: 文档
permalink: /zh/docs/handbook/declaration-files/templates/module-class-d-ts.html
---

<!--
TODO:

1. Not clear why UMD is thrown in here.
2. Give both commonjs and ES module examples.
1. 不清楚为什么在这里加入 UMD
2. 给出 CommonJS 和 ES 模块的示例。
-->

For example, when you want to work with JavaScript code which looks like:
例如,当你希望使用类似以下 JavaScript 代码时:

```ts
const Greeter = require("super-greeter");
Expand All @@ -20,44 +20,43 @@ const greeter = new Greeter();
greeter.greet();
```

To handle both importing via UMD and modules:
处理通过 UMD 和模块导入的情况:

```ts
// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>
// 类型定义为 [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// 项目: [~THE PROJECT NAME~]
// 定义者: [~YOUR NAME~] <[~A URL FOR YOU~]>

/*~ This is the module template file for class modules.
*~ You should rename it to index.d.ts and place it in a folder with the same name as the module.
*~ For example, if you were writing a file for "super-greeter", this
*~ file should be 'super-greeter/index.d.ts'
/*~ 这是用于类模块的模块模板文件。
*~ 你应该将其重命名为 index.d.ts,并将其放在与模块同名的文件夹中。
*~ 例如,如果你为 "super-greeter" 编写一个文件,此
*~ 文件应该是 'super-greeter/index.d.ts'
*/

// Note that ES6 modules cannot directly export class objects.
// This file should be imported using the CommonJS-style:
// 请注意,ES6 模块无法直接导出类对象。
// 该文件应该使用 CommonJS 风格导入:
// import x = require('[~THE MODULE~]');
//
// Alternatively, if --allowSyntheticDefaultImports or
// --esModuleInterop is turned on, this file can also be
// imported as a default import:
// 或者,如果 --allowSyntheticDefaultImports 或
// --esModuleInterop 打开,该文件也可以作为默认导入导入:
// import x from '[~THE MODULE~]';
//
// Refer to the TypeScript documentation at
// 请参阅 TypeScript 文档
// https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
// to understand common workarounds for this limitation of ES6 modules.
// 了解 ES6 模块的此限制的常见解决方法。

/*~ If this module is a UMD module that exposes a global variable 'myClassLib' when
*~ loaded outside a module loader environment, declare that global here.
*~ Otherwise, delete this declaration.
/*~ 如果此模块是一个 UMD 模块,在加载到模块加载器环境之外时公开全局变量 'myClassLib'
*~ 请在此处声明该全局变量。
*~ 否则,请删除此声明。
*/
export as namespace "super-greeter";

/*~ This declaration specifies that the class constructor function
*~ is the exported object from the file
/*~ 此声明指定类构造函数
*~ 是文件中导出的对象
*/
export = Greeter;

/*~ Write your module's methods and properties in this class */
/*~ 在此类中编写你模块的方法和属性 */
declare class Greeter {
constructor(customGreeting?: string);

Expand All @@ -66,13 +65,13 @@ declare class Greeter {
myMethod(opts: MyClass.MyClassMethodOptions): number;
}

/*~ If you want to expose types from your module as well, you can
*~ place them in this block.
/*~ 如果你还想公开模块的类型,你可以
*~ 将它们放在此块中。
*~
*~ Note that if you decide to include this namespace, the module can be
*~ incorrectly imported as a namespace object, unless
*~ --esModuleInterop is turned on:
*~ import * as x from '[~THE MODULE~]'; // WRONG! DO NOT DO THIS!
*~ 请注意,如果决定包含此命名空间,则可能会
*~ 错误地将模块导入为命名空间对象,除非
*~ 打开了 --esModuleInterop
*~ import * as x from '[~THE MODULE~]'; // 错误!不要这样做!
*/
declare namespace MyClass {
export interface MyClassMethodOptions {
Expand Down
Loading