0
votes

The Setup:
I created a TypeScript file (String.ts) to extend string:

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 };

The tsconfig.json files that I created contain:

{
  "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"
  ]
}

File Structure:

  • Common (folder)
    ~ String.ts
    ~ <other files>
    ~ tsconfig.json
  • Bussines (folder)
    ~ app.ts --> here we use String.IsNullOrWhiteSpace
    ~ <other files>
    ~ tsconfig.json
  • tsconfig.json

The problem(in the following order)
1) When I have one tsconfig.json (highest level), everything compiles ok
2) When I add a tsconfig.json in Common, everything compiles ok
3) When I add a tsconfig.json in Business, I get a compilation error that String.isNullOrWhiteSpace cannot be found.

Anyone knows what the problem could be and how to solve it?

(If more information is needed... let me know!)

1
Why do you need multiple tsconfigs?Spitzbueb
For example to compile all my files within the Common folder into one file and improve performance. And still keep good maintainable code by splitting them into multiple filesRubenHerman
I also like to split up my interfaces in separate files, but when we compile this, we get an empty *.js file --> So compiling everything in one file solves this. But if we compile EVERYTHING to this one file (on highest level), there is too much in there to reuse it in other applications as wellRubenHerman
Interfaces don't get compiled, thats correct. They are (like types) only usable in Typescript and not Javascript (meaning you can only validate the type or interface during compilation and not afterwards). Could you show us the code in the file you can't compile?Spitzbueb

1 Answers

1
votes

String.ts is not included in compilation when you segregate the Business directory(ie it would need to be imported from elsewhere). You should have a separate common directory called for interfaces and common definitions.