Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在编译但在运行时不工作的Typescript扩展方法_Javascript_Node.js_Typescript_Ecmascript 6 - Fatal编程技术网

Javascript 正在编译但在运行时不工作的Typescript扩展方法

Javascript 正在编译但在运行时不工作的Typescript扩展方法,javascript,node.js,typescript,ecmascript-6,Javascript,Node.js,Typescript,Ecmascript 6,我用一个简单的方法扩展了Typescript中的字符串原型: StringExtensions.ts String.prototype.toCleanedTitleCase = function ():string { let titleCase = this.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() })

我用一个简单的方法扩展了Typescript中的字符串原型:

StringExtensions.ts

String.prototype.toCleanedTitleCase = function ():string {
    let titleCase = this.replace(/\w\S*/g, function (txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
    })
    return titleCase.replace(new RegExp(" ", 'g'), "").replace(new RegExp("-", 'g'), "")
}
interface String {
    toCleanedTitleCase():String;
}
console.log(("Some string that needs a-sorting").toCleanedTitleCase();
然后在定义文件中添加了一个接口:

StringExtensions.d.ts

String.prototype.toCleanedTitleCase = function ():string {
    let titleCase = this.replace(/\w\S*/g, function (txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
    })
    return titleCase.replace(new RegExp(" ", 'g'), "").replace(new RegExp("-", 'g'), "")
}
interface String {
    toCleanedTitleCase():String;
}
console.log(("Some string that needs a-sorting").toCleanedTitleCase();
然后称之为:

index.ts

String.prototype.toCleanedTitleCase = function ():string {
    let titleCase = this.replace(/\w\S*/g, function (txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
    })
    return titleCase.replace(new RegExp(" ", 'g'), "").replace(new RegExp("-", 'g'), "")
}
interface String {
    toCleanedTitleCase():String;
}
console.log(("Some string that needs a-sorting").toCleanedTitleCase();
所有的编译都很好,但是当我运行我的应用程序(NodeJS)时,我得到的“xxx.toCleanedTitleCase”不是一个函数


你知道我遗漏了什么吗?

这只是一个例子,在普通字符串上得到相同的错误你检查了编译的js以确保函数确实存在吗?我的猜测是,您既不是AMD的安装程序,也不是编译为单个输出文件,因此StringExtensions内容没有进入输出。如果文件中有一个//,它仍然会编译。就是这样。将require(./StringExtensions)添加到我的索引顶部修复了它,您将require(./StringExtensions)放在哪里了?到你索引的顶端?这是哪里?你的指数是多少?如果我在index.ts中放入类似的行,它甚至不会在编译时编译。`“路径”:{“@extensions/*”:[“src/extensions/*.extensions.ts”]}`和
import'@extensions/array'在我使用的扩展名方法适用的文件中