TypeScript:如何将类型声明绑定到导入语句
TypeScript: How to bind type declaration to import statement
我正在尝试为 es6-promisify, a JS package not in the DefinitelyTyped annotations repository. Looking at examples in DefinitelyTyped and following TS Deep Dive / Declaration Files 创建一个类型定义,我创建了一个粗略的注释并保存在我的项目中 vendor.d.ts
:
declare function promisify(original: (...args: any[]) => any, settings: any): Promise<any>
export = promisify
// export interface promisify { } // or should I do an interface?
现在,考虑到我使用 import promisify = require('es6-promisify')
导入,我如何告诉 TypeScript promisify
导入在 vendor.d.ts
中注释?目前,tsc
不断返回 Could not find a declaration file for module 'es6-promisify'. 'promisify.js' implicitly has an 'any' type.
我正在尝试消化 TS Docs / Module Resolution 但到目前为止我还是失败了。
换句话说:TypeScript 使用什么机制从导入解析声明文件? XY problem 警告:也许我做错了,不应该做 vendor.d.ts
?也许 es6-promisify
不在 DT 中是有充分理由的?随意与更好的方法相矛盾,以实现我的目标,让 "noImplicitAny": true
的 tsc 快乐。谢谢:)
以下适合我。
es6-promisifiy.d.ts
:
declare module "es6-promisify" {
export default function promisify(original: (...args: any[]) => any, settings: any): Promise<any>
}
用法:
import promisify from "es6-promisify";
...
const xyz = promisify(whatever, whatever);
tsconfig.json:
{
"compilerOptions": {
...
"typeRoots": [
"./node_modules/@types",
"./custom_typings"
]
},
...
}
希望对您有所帮助。
我正在尝试为 es6-promisify, a JS package not in the DefinitelyTyped annotations repository. Looking at examples in DefinitelyTyped and following TS Deep Dive / Declaration Files 创建一个类型定义,我创建了一个粗略的注释并保存在我的项目中 vendor.d.ts
:
declare function promisify(original: (...args: any[]) => any, settings: any): Promise<any>
export = promisify
// export interface promisify { } // or should I do an interface?
现在,考虑到我使用 import promisify = require('es6-promisify')
导入,我如何告诉 TypeScript promisify
导入在 vendor.d.ts
中注释?目前,tsc
不断返回 Could not find a declaration file for module 'es6-promisify'. 'promisify.js' implicitly has an 'any' type.
我正在尝试消化 TS Docs / Module Resolution 但到目前为止我还是失败了。
换句话说:TypeScript 使用什么机制从导入解析声明文件? XY problem 警告:也许我做错了,不应该做 vendor.d.ts
?也许 es6-promisify
不在 DT 中是有充分理由的?随意与更好的方法相矛盾,以实现我的目标,让 "noImplicitAny": true
的 tsc 快乐。谢谢:)
以下适合我。
es6-promisifiy.d.ts
:
declare module "es6-promisify" {
export default function promisify(original: (...args: any[]) => any, settings: any): Promise<any>
}
用法:
import promisify from "es6-promisify";
...
const xyz = promisify(whatever, whatever);
tsconfig.json:
{
"compilerOptions": {
...
"typeRoots": [
"./node_modules/@types",
"./custom_typings"
]
},
...
}
希望对您有所帮助。