使用TypeScript和Babel Polyfill会导致编译错误。

4
我正在使用babel-polyfill和typescript。
当我使用string.includes()函数时,会出现错误。

Error TS2339:属性'includes'在类型'string'上不存在

stackoverflow中有一个类似的问题:error TS2339: Property 'endsWith' does not exist on type 'string' 那里的答案是扩展自定义接口String。
interface String {    
    endsWith(searchString: string, endPosition?: number): boolean;
};

我不太喜欢这个想法,因为这意味着我将不得不以这种方式修复每个es6 polyfill特性。

有没有一种方法告诉typescript应该使用es6定义,但是编译到es5?

1个回答

3

没有一个编译选项可以打开此功能,但是您可以直接引用lib.es6.d.ts文件:

///<reference path="../path/to/node_modules/typescript/lib/lib.es6.d.ts" />

或者使用像es6-shim这样的shim库获取类型,但不包括js文件,因为babel会为您进行转译。

tsd install es6-shim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接