简体   繁体   English

类模块的声明文件(TypeScript)

[英]Declaration file for class-module (TypeScript)

I use a node module that's not found by typings and doesn't exist in definelytyped. 我使用的节点模块无法通过键入找到,并且在definelytyped中不存在。

the basic use of the module is: 该模块的基本用途是:

import * as SomeClass from 'some-module';

var someObject = new SomeClass("some string");
someObject.someMethod();

As you can see, this module exports a class as its default. 如您所见,此模块导出一个类作为其默认值。 I haven't figured out how to write the declaration file for it. 我还没有弄清楚如何为它编写声明文件。

This is the best I managed to do: 这是我设法做到的最好:

declare module 'some-module' {
    export default class SomeClass {
        constructor (someArg: string);
        someMethod(): void;
    }
}

BTW it does work JavaScriptly. 顺便说一句,它确实可以使用JavaScript。 It's only the TypeScript bothers me. 只是TypeScript困扰我。

Any ideas how to solve that? 任何想法如何解决?

For the declaration, you need to do this: 对于声明,您需要执行以下操作:

declare module 'some-module' {
    class SomeClass {
        constructor (someArg: string);
        someMethod(): void;
    }
    namespace SomeClass {}
    export = SomeClass;
}

UPDATE: Thank you to Blake Embrey for pointing out, the hack namespace SomeClass {} is needed to get this working. 更新:感谢布莱克·恩布雷(Blake Embrey)指出,需要使用hack namespace SomeClass {}来使此工作正常。 see https://github.com/Microsoft/TypeScript/issues/5073 for more detail. 有关更多详细信息,请参阅https://github.com/Microsoft/TypeScript/issues/5073

如果您只是想避免TypeScript错误并且不担心ItelliSense,只需像在文件开头那样声明类库即可,例如:

declare var SomeClass: any;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript声明文件不是模块 - Typescript declaration file is not a module 功能模块的声明文件(TypeScript) - Declaration file for function-module (TypeScript) 打字稿:找不到模块“react-cards”的声明文件 - Typescript: Could not find a declaration file for module 'react-cards' TypeScript 错误“找不到模块的声明文件”-无法修复 - TypeScript error "Could not find a declaration file for module" - unable to fix 如何在TypeScript声明文件中设置默认的类属性值? - How to set default class property value in TypeScript declaration file? 如何在打字稿声明文件中定义单例javascript类 - How to define a singleton javascript class in a typescript declaration file 如何找到我的打字稿/反应模块的声明? - How to find declaration for my typescript/react module? Cloudinary-React 找不到模块“cloudinary-react”的声明文件 - 不使用 TypeScript - Cloudinary-React Could not find a declaration file for module 'cloudinary-react' - Not using TypeScript Typescript:使用 dts-gen 后找不到模块的声明文件 - Typescript: could not find a declaration file for module after using dts-gen 在 TypeScript 中使用 SweetAlert2,找不到模块“sweetalert2/dist/sweetalert2.js”的声明文件 - Using SweetAlert2 with TypeScript, could not find a declaration file for module 'sweetalert2/dist/sweetalert2.js'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM