DefinitelyTyped有Highlight.js的定义,它定义并导出一个模块,如下所示:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == segueName {
let productsController = segue.destinationViewController as! ProductsController
productsController.products = sender as! [Product]
}
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let company = DataService.allCompanies[indexPath.item]
DataService.getAllProductsForCompanies([company]) { (products: [Product]) -> () in
dispatch_async(dispatch_get_main_queue()) {
print("getAllProductsForCompanies for companyId: \(company.companyId)")
self.performSegueWithIdentifier(self.segueName, sender: products as? AnyObject)
}
}
}
在给定的打字稿文件中,我试图导入declare module "highlight.js"
{
module hljs
{
export function highlight(
name: string,
value: string,
ignore_illegals?: boolean,
continuation?: boolean) : IHighlightResult;
...
}
export = hljs;
}
对象,以便我可以在其上调用hljs
函数,如下所示:
highlight
但它没有说/// <reference path="../../tsd_typings/highlightjs/highlightjs.d.ts" />
import {hljs} from 'highlight.js';
...
hljs.highlightBlock(block);
导入此对象的正确方法是什么,以便我可以编译我的TS文件而不会出现错误和警告?
答案 0 :(得分:2)
import语句应为:
import * as hljs from 'highlight.js';
这相当于:
import hljs = require('highlight.js');
并将整个模块导入hljs
。