【问题标题】:How to configure tsconfig.json to support string literals or string templates如何配置 tsconfig.json 以支持字符串文字或字符串模板
【发布时间】:2018-01-29 19:38:02
【问题描述】:

我使用 swagger codgen 和 typescript-aurelia 模板来生成 API 代码。结果代码大量使用了字符串文字。命令行 transpolar tsc 会抛出有关此代码的错误,但会输出 javascript,运行时不会出错。

但是,我在较大的构建任务中使用 gulp-typescript,当它遇到 error TS2304: Cannot find name '$'. 这样的代码时,它不会输出 javascript 文件:

const url = `${this.basePath}/api/v2/courses/{courseId}`
      .replace(`{${'courseId'}}`, encodeURIComponent(String(${params['courseId']})));

我相信它是完全有效的 TypeScript;但是,可能有一个编译器选项来支持字符串文字/模板。只是还没找到。

有谁知道如何抑制或消除这些错误?

【问题讨论】:

    标签: typescript string-literals tsconfig


    【解决方案1】:

    我认为问题不在于 TypeScript 不支持字符串文字/模板。

    相反,您的最终模板似乎缺少反引号。

    你在写:

    encodeURIComponent(String(${params['courseId']}))
    

    什么时候应该是:

    encodeURIComponent(String(`${params['courseId']}`))
    

    或者干脆

    encodeURIComponent(String(params['courseId']))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 2014-10-12
      相关资源
      最近更新 更多