【发布时间】:2015-02-28 08:19:15
【问题描述】:
我正在将模块引入现有的打字稿项目,以便它可以使用外部模块。当前代码扩展了基本类型,如字符串,无需模块即可正常工作。一旦我引入导入,编译就会失败。
内部模块失败:
/// <reference path='../defs/react.d.ts' />
import React = require("react");
module ExampleModule {
interface String {
StartsWith: (str : string) => boolean;
}
if (typeof String.prototype.StartsWith !== 'function') {
String.prototype.StartsWith = function(str) {
return this.slice(0, str.length) === str;
};
}
export function foo() { return "sdf".StartsWith("s"); }
}
外部模块失败:
/// <reference path='../defs/react.d.ts' />
import React = require("react");
interface String {
StartsWith: (str : string) => boolean;
}
if (typeof String.prototype.StartsWith !== 'function') {
String.prototype.StartsWith = function(str) {
return this.slice(0, str.length) === str;
};
}
module ExampleModule {
export function foo() { return "sdf".StartsWith("s"); }
}
但如果你删除导入,那么它工作正常:
interface String {
StartsWith: (str : string) => boolean;
}
if (typeof String.prototype.StartsWith !== 'function') {
String.prototype.StartsWith = function(str) {
return this.slice(0, str.length) === str;
};
}
module ExampleModule {
export function foo() { return "sdf".StartsWith("s"); }
}
错误发生在这一行:
if (typeof String.prototype.StartsWith !== 'function') {
并阅读:
The property 'StartsWith' does not exist on value of type 'String'
【问题讨论】:
标签: module typescript