问题描述
我有一个Node C ++插件,它提供了一个类似于Node文档中的包装类.我可以使用require()我的插件,然后获取我的类的构造函数以创建实例.
I have a Node C++ Addon that provides a wrapped class similar to the one in the Node documentation. I can require() my addon and then get the constructor for my class to create an instance.
const { MyClass } = require('myaddon');
const obj = new MyClass('data');
现在,我想使用TypeScript进行相同的操作.我找不到.d.ts文件和import语句的正确组合来完成这项工作.我想理想情况下,我想声明我的类在模块中,并且有一个采用字符串的构造函数.然后我可以这样做:
Now I want to use TypeScript to do the same. I can't find the right combination of .d.ts file and import statement to make this work. I guess ideally I'd like to declare my class is in the module and has a constructor that takes a string. I could then just do:
import { MyClass } from 'myaddon';
const obj = new MyClass('data');
人们看到过这样的例子吗?
Any examples of this that people have seen?
推荐答案
我想我终于有了.按照@ZachB的建议,我创建了一个myaddon.ts文件,该文件具有以下内容:
I think I finally have it. As suggested by @ZachB, I created a myaddon.ts file that has the following:
const myaddon = require('./build/release/myaddon')
export interface MyClass {
myMethod(arg: string): number
}
export var MyClass: {
new(param: string): MyClass
} = myaddon.MyClass
然后使用它:
import { MyClass } from 'myaddon'
const thing: MyClass = new MyClass('something')
const answer: number = thing.myMethod('blah')
这篇关于Node C ++插件的Typescript声明文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!