我已经阅读了文档(即this)和一些关于declareTypeScript 中关键字的博客文章,但我仍然不明白。我想要的是其最简单用法的清晰示例。
这是我最初想出这个最简单的例子(这可能是不正确的)的推理路线:
有一个.js文件导出(无类型,因为这是普通的 JS)。
.ts无论.js导出的文件如何,都有一个文件导入。
需要导出和导入什么(一个对象?一个函数?)让我看到 TS 显示可以通过使用修复的错误declare?
你的例子是正确的。例如,您使用的是用纯 javascript 编写的节点模块(没有可用的类型),因此 tscompiler 会注意到(因为它搜索通常在节点模块或额外的类型中的类型@typings/package)。
但是,您可以通过告诉 tscompilertsconfig.json查看文件中的类型来自己提供这些类型,xyz.d.ts例如
tsconfig.json
{
"files": [
"typings/index.d.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
这index.d.ts是收集所有自定义类型的地方,看起来像这样
index.d.ts
/// <reference path="custom-typings.d.ts" />
Run Code Online (Sandbox Code Playgroud)
并且里面custom-typings.d.ts有实际的类型。这就是declare关键字发挥作用的地方
custom-typings.d.ts
declare module "the-untyped-node-module-name" {
export default class TheNodeModuleClass {
static showNotification(string: any): void;
}
}
Run Code Online (Sandbox Code Playgroud)
现在打字稿编译器知道有一个TheNodeModuleClass在the-untyped-node-module-name其中具有静态函数showNotification。
有关更多信息,请参阅 Typscript 模块。
这是关键字的一个用例declare。当然还有更多的是喜欢declare var,declare function,declare class等等。
Murat Karag\xc3\xb6z 上面的答案指出了正确的方向,这个答案将提供实际的代码以及我们将在哪里使用declare.
这是一个非常简单的 npm 模块:只有一个index.js文件,导出一个带有一种方法的对象。这里没有类型声明,因为它只是 JS。
const item = {\n price: 5,\n name: \'item1\',\n};\n\nexport const MyModuleObject = { method: () => item };\nRun Code Online (Sandbox Code Playgroud)\n\n这是一个非常简单的 TypeScript npm 项目,具有一个依赖项:上面链接的 JS 项目。因此,后者是一个导入的 npm 模块,没有任何类型。这是TS项目中的index.ts文件:
\n\n/// <reference path="index.d.ts"/>\n\nimport { MyModuleObject } from \'npmModule\';\n\nimport { LocalModuleObject } from \'./module\';\n\n// Without the "reference path" line, TS complains in line 3 that it could not find a declaration file for \'npmModule\'.\n// In this case, the import has type any, so TS does not complain about the call below to an inexistent method.\n// When we uncomment line 1, we get a TS error on line 8: property \'test\' does not exist on type { method: ... }\nMyModuleObject.test();\n\n\n// TS complains that test does not exist on type { method: ... }\n// Here we did not need to have a `declare` statement in a type definitions file for TS to know this because here TS is\n// using contextual typing:\nLocalModuleObject.test();\nRun Code Online (Sandbox Code Playgroud)\n\n下面是代码index.d.ts:
declare module "npmModule" {\n export const Item: {\n price: number,\n name: string\n }\n\n export const MyModuleObject: {\n method: () => Item\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n以及代码./module:
export const LocalModuleObject = { method: () => 10 };\nRun Code Online (Sandbox Code Playgroud)\n\n这是为什么使用声明的示例\n- 我将其放在 的注释中index.ts,但让我用更多的话来解释它。index.ts正在从外部模块( 中的一个node_modules)导入一个对象,并从本地模块(./module.js)导入另一个对象。两个模块都导出一个对象,并调用method该对象的一种方法。我正在对每个对象index.ts调用一个不存在的方法。test
TS 在导入本地模块时使用上下文类型,因此它知道test对象上不存在该模块。在外部模块中导入对象时不会发生这种情况:此导入是使用 type 导入的any。因此,TS不会抱怨调用不存在的方法test。然而,它确实抱怨没有外部模块的类型,因此这是any正在使用隐式的暗示。
index.d.ts我们可以通过定义一个为外部库提供类型的方法来解决后一个问题。这是declare module使用的地方:它声明模块导出的内容npmModule;npmModule是外部导入。我们index.ts必须添加这一行,/// <reference path="index.d.ts"/>以便 TS 知道在哪里寻找类型。
| 归档时间: |
|
| 查看次数: |
287 次 |
| 最近记录: |