我正在使用 babel-polyfill 和 typescript。
当我使用函数 string.includes() 时出现错误
Error TS2339: Property 'includes' does not exist on type 'string'
stack overflow也有类似的问题:error TS2339: Property 'endsWith' does not exist on type 'string'
答案是扩展自定义接口(interface)String
interface String {
endsWith(searchString: string, endPosition?: number): boolean;
};
我不太喜欢这个想法,因为这意味着我将不得不以这种方式修复每个 es6 polyfill 功能。
有没有办法告诉 typescript 他应该使用 es6 定义,但转译为 es5?
最佳答案
没有编译器选项可以打开它,但是您可以直接引用 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
关于typescript - 使用 typescript 和 babel-polyfill 会导致编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34987877/