打字稿“导出外部包装类型不是模块”

时间:2016-04-09 09:29:54

标签: javascript typescript

我正在尝试创建我的第一个Typescript定义文件。主题代码是filename.js(index.js):

module.exports = {
  extension: extension,
  basename: basename,
  removeSuffix: removeSuffix,
  removeSuffixWithDelimiter: removeSuffixWithDelimiter,
  appendSuffix: appendSuffix,
  appendSuffixWithDelimiter: appendSuffixWithDelimiter,
  directoryName: directoryName
}
function extension(filename) {}
function basename(filename) {}
function removeSuffix(filename) {}
function removeSuffixWithDelimiter(delimiter, filename) {}
function appendSuffix(suffix, filename) {}
function appendSuffixWithDelimiter(suffix, delimiter, filename) {}
function directoryName(filename) {}

我到目前为止的定义(index.d.ts)文件:

declare module "filename.js" {
  export function extension(filename: string): string;
  export function basename(filename: string): string;
  export function removeSuffix(filename: string): string;
  export function removeSuffixWithDelimiter(delimiter: string|number, filename: string): string;
  export function appendSuffix(suffix: string, filename: string): string;
  export function appendSuffixWithDelimiter(suffix: string, delimiter: string|number, filename: string): string;
  export function directoryName(filename: string): string;
}

这很好用(在我的编辑器中自动完成工作),但是我收到编译错误:

  

index.ts(21,29):错误TS2656:导出的外部包类型文件'filename.js / index.d.ts'不是模块。请与软件包作者联系以更新软件包定义。

这个错误是什么意思(对Typescript来说是新的),更重要的是,我应该如何修改我的定义以使其更正确?

1 个答案:

答案 0 :(得分:1)

您提供的数据的定义文件可以是: -

declare module customTypings{
 interface filename{
  extension(filename: string): string;
  basename(filename: string): string;
  removeSuffix(filename: string): string;
  removeSuffixWithDelimiter(delimiter: string|number, filename: string): string;
  appendSuffix(suffix: string, filename: string): string;
  appendSuffixWithDelimiter(suffix: string, delimiter: string|number, filename: string): string;
  directoryName(filename: string): string;
 }
}

您可以通过指定: -

来使用此输入文件
/// <reference path="pathName" />

看看它是否有效。

有关详细信息,请参阅已创建的.d.ts文件。例如angular-material.d.ts