2016-04-09 100 views
0

我想創建我的第一個Typescript定義文件。主題代碼是filename.js(index.js):Typescript「導出的外部封裝類型不是一個模塊」

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): 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.

這個錯誤是什麼意思(新打字稿),更重要的是,我應該怎麼修改我的定義,使之更正確?

+0

你能解決這個問題嗎?我有同樣的問題。 – emzero

+0

@emzero no我目前還沒有能力 –

+0

希望它會在TS 2.0中修復 – emzero

回答

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

+0

不幸的是,同樣的錯誤。你的例子也不提供準確的自動完成。 –

+0

您是否看到過已創建的.d.ts文件。可能是我們應該在創建我們時引用這些文件,以便我們不在代碼中包含任何額外的東西。 – Ajay

+0

我在問題中包含了我的index.d.ts文件。 –

相關問題