【问题标题】:TypeScript: Unable to find reference when compilingTypeScript:编译时找不到参考
【发布时间】:2017-07-31 07:45:00
【问题描述】:

设置:
我创建了一个 TypeScript 文件 (String.ts) 来扩展字符串:

interface StringConstructor {
    isNullOrEmpty(str: string): boolean;
    isNullOrWhiteSpace(str: string): boolean;
}

String.isNullOrEmpty = (str: string) => !str;
String.isNullOrWhiteSpace = (str: string) => { return (String.isNullOrEmpty(str)) ? false : str.replace(/\s/g, '').length < 1 };

我创建的 tsconfig.json 文件包含:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "strict": true,
    "noEmitOnError": true,
    "module": "amd",
    "removeComments": true,
    "target": "es5",
    "declaration": true,
    "outFile": "./Common.js", --> Only this line changes in other tsconfig.json files (+ does not exist in highest located tsconfig.json file)
    "inlineSourceMap": true,
    "inlineSources": true,
    "lib": [
      "dom",
      "es5",
      "es2015.promise"
     ]
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

文件结构:

  • 通用(文件夹)
    ~ 字符串.ts
    ~
    ~ tsconfig.json
  • 商务(文件夹)
    ~ app.ts --> 这里我们使用 String.IsNullOrWhiteSpace
    ~
    ~ tsconfig.json
  • tsconfig.json

问题(按以下顺序)
1)当我有一个 tsconfig.json(最高级别)时,一切都编译正常
2) 当我在 Common 中添加一个 tsconfig.json 时,一切都编译好了
3)我在Business中添加tsconfig.json时,编译报错String.isNullOrWhiteSpace 找不到。

有人知道问题可能是什么以及如何解决吗?

(如果需要更多信息……请告诉我!)

【问题讨论】:

  • 为什么需要多个tsconfigs
  • 例如将Common文件夹中的所有文件编译成一个文件并提高性能。并且仍然通过将它们分成多个文件来保持良好的可维护代码
  • 我也喜欢将我的接口拆分到单独的文件中,但是当我们编译它时,我们会得到一个空的 *.js 文件 --> 所以将所有内容编译到一个文件中就可以解决这个问题。但是,如果我们将所有内容都编译到这个文件中(最高级别),那么其中的内容太多,无法在其他应用程序中重用它
  • 接口没有被编译,这是正确的。它们(就像类型一样)只能在 Typescript 中使用,而不能在 Javascript 中使用(这意味着您只能在编译期间验证类型或接口,而不能在之后验证)。你能告诉我们你不能编译的文件中的代码吗?

标签: json typescript tsconfig


【解决方案1】:

当您分离业务目录时,String.ts 不包含在编译中(即,它需要从其他地方导入)。您应该有一个单独的公共目录,用于调用接口和公共定义。

【讨论】:

  • 我可以在我的 tsconfig.json 中使用“include”来解决这个问题吗? (如果是,怎么做?)
  • 使用“导入”即import '../Common/String'
猜你喜欢
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多