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 global modyfing module #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,72 +1,67 @@
---
title: "Global: Modifying Module"
title: "全局修改模块"
layout: docs
permalink: /zh/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html
---

## _Global-modifying Modules_
## 全局修改模块

A _global-modifying module_ alters existing values in the global scope when they are imported.
For example, there might exist a library which adds new members to `String.prototype` when imported.
This pattern is somewhat dangerous due to the possibility of runtime conflicts,
but we can still write a declaration file for it.
全局修改模块在导入时会更改全局作用域中的现有值。例如,可能存在一个库,在导入时会向 `String.prototype` 添加新成员。这种模式有一定的风险,因为可能会发生运行时冲突,但我们仍然可以为其编写声明文件。

## Identifying global-modifying modules
## 识别全局修改模块

Global-modifying modules are generally easy to identify from their documentation.
In general, they're similar to global plugins, but need a `require` call to activate their effects.
全局修改模块通常可以通过其文档轻松识别。一般来说,它们类似于全局插件,但需要一个 `require` 调用来激活其效果。

You might see documentation like this:
你可能会看到如下文档:

```js
// 'require' call that doesn't use its return value
// 不使用返回值的 'require' 调用
var unused = require("magic-string-time");
/* or */
/* */
require("magic-string-time");

var x = "hello, world";
// Creates new methods on built-in types
// 在内置类型上创建新方法
console.log(x.startsWithHello());

var y = [1, 2, 3];
// Creates new methods on built-in types
// 在内置类型上创建新方法
console.log(y.reverseAndSort());
```

Here is an example
## 示例

```ts
// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>
// [~库名称~] [~可选版本号~] 的类型定义
// 项目: [~项目名称~]
// 定义者: [~你的姓名~] <[~你的网址~]>

/*~ This is the global-modifying module template file. 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: If your global-modifying module is callable or constructable, you'll
*~ need to combine the patterns here with those in the module-class or module-function
*~ template files
/*~ 注意:如果你的全局修改模块是可调用或可构造的,
*~ 你需要将这里的模式与模块类或模块函数模板文件中的模式结合
*/
declare global {
/*~ Here, declare things that go in the global namespace, or augment
*~ existing declarations in the global namespace
/*~ 在这里声明放入全局命名空间的内容,或增强
*~ 全局命名空间中现有声明
*/
interface String {
fancyFormat(opts: StringFormatOptions): string;
}
}

/*~ If your module exports types or values, write them as usual */
/*~ 如果你的模块导出类型或值,请按常规方式编写 */
export interface StringFormatOptions {
fancinessLevel: number;
}

/*~ For example, declaring a method on the module (in addition to its global side effects) */
/*~ 例如,在模块上声明一个方法(除了其全局副作用) */
export function doSomething(): void;

/*~ If your module exports nothing, you'll need this line. Otherwise, delete it */
/*~ 如果你的模块不导出任何内容,你需要这一行。否则,请删除它 */
export {};
```
Loading