【问题标题】:using typescript and babel-polyfill results in compiler errors使用 typescript 和 babel-polyfill 会导致编译器错误
【发布时间】:2016-05-01 11:36:21
【问题描述】:

我正在使用 babel-polyfill 和 typescript。

当我使用函数 string.includes() 时出现错误

错误 TS2339:“字符串”类型上不存在属性“包含”

堆栈溢出也有类似问题:error TS2339: Property 'endsWith' does not exist on type 'string'

答案是扩展自定义接口String

interface String {    
    endsWith(searchString: string, endPosition?: number): boolean;
};

我不太喜欢这个想法,因为这意味着我必须以这种方式修复每个 es6 polyfill 功能。

有没有办法告诉 typescript 他应该使用 es6 定义,但转译为 es5?

【问题讨论】:

    标签: typescript babeljs


    【解决方案1】:

    没有编译器选项可以打开它,但是您可以直接引用lib.es6.d.ts 文件:

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

    或使用像es6-shim 这样的垫片库来获取类型,但不要包含 js 文件,因为 babel 会为你转译它。

    tsd install es6-shim
    

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 1970-01-01
      • 2017-04-30
      • 2015-10-03
      • 2015-12-16
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多