如何使用默认函数导出为 commonjs 模块创建类型
How to create typings for a commonjs module with a default function export?
这是我正在为其键入的代码:
https://github.com/jden/objectid/blob/1.1.0/index.js
到目前为止我尝试过的 -
尝试 #1:
declare module "objectid" {
interface ObjectId {
(): string
isValid(objectId: string): boolean
}
export default ObjectId
}
...
import makeObjectId from 'objectid' // Error TS2304: Cannot find name 'makeObjectId'
尝试#2:
declare module "objectid" {
interface ObjectId {
(): string
isValid(objectId: string): boolean
}
export = ObjectId
}
...
import makeObjectId = require('objectid')
const id = makeObjectId() // Error TS2304: Cannot find name 'makeObjectId'
尝试 #3:
declare module "objectid" {
export default function makeObjectId(): string
export function isValid(objectId: string): boolean
}
...
import makeObjectId = require('objectid')
const id = makeObjectId() // TypeError: objectid_1.default is not a function
编辑:任何将来发现这一点的人的工作解决方案:
declare module "objectid" {
interface ObjectId {
(): string
isValid(objectId: string): boolean
}
declare var objectId: ObjectId
export = objectId
}
...
import * as makeObjectId from 'objectid'
const id = makeObjectId()
您可以尝试在 d.ts 文件中像这样声明它:
declare module "objectid"
{
interface ObjectId
{
(): string
isValid(objectId: string): boolean
}
var foo: ObjectId;
export default foo;
}
相关文章:
- 如何捆绑commonjs模块,除非指定需要
- 如何在JSFiddle上使用CommonJS模块
- 如何使用默认函数导出为 commonjs 模块创建类型
- 定义CommonJS模块时,fn.call(this)与fn()的对比
- 可以Typescript导入CommonJS模块
- Typescript:从CommonJS模块导入默认值,从打字文件导出附加类型
- 打包具有依赖关系的Browser/Server CommonJS模块
- 如何将一个没有模块的js应用程序迁移到commonJs模块
- 闭包编译器可以构建CommonJS模块
- 当作为 CommonJS 模块加载时,Angular 是否全局将自己分配给“window.angular”
- 如何将 CommonJS 模块捆绑到单个 UMD 库文件中
- Java 7 + Rhino 1.7R3 支持 CommonJS 模块
- When是通过node js使用CommonJS模块化时执行的Javascript构造函数
- Backbone是如何做到的.$在CommonJS模块配置中设置
- 我想在commonjs模块中使用全局jQuery
- 通过intern对浏览器CommonJS模块进行单元测试
- 不进行异步调用的CommonJS模块加载程序
- module.exports"和“;exports"在CommonJs模块系统中
- 如何将我的CommonJS模块转换为UMD
- 我怎么能要求'浏览器中的CommonJS模块