简体   繁体   中英

Typescript “exported external package typings is not a module”

I'm trying to create my first Typescript definition file. The subject code is 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) {}

The definition (index.d.ts) file I have so far:

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;
}

This works well enough (auto complete works in my editor), but I get a compile error:

index.ts(21,29): error TS2656: Exported external package typings file 'filename.js/index.d.ts' is not a module. Please contact the package author to update the package definition.

What does this error mean (new to Typescript), and more importantly, how should I modify my definition to make it more correct?

Definition file for the data provided by you can be:-

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;
 }
}

You can use this typing file by specifying:-

/// <reference path="pathName" />

See if it works.

For more info refer already created .d.ts files. eg angular-material.d.ts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM