TypeScript: Project References don't resolve ambient Definitions

TypeScript Version: 3.3.0-dev.20181213

Search Terms: Project References Composite Project Ambient Definitions

Code (minimum example, see repository link below for complete setup) tsconfig_base.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "none",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "strict": true,
    "composite": true
  }
}

utils/tsconfig.json

{
  "extends": "../tsconfig_base.json",
  "include": [
    "*.ts",
    "*.d.ts"
  ]
}

utils/utils.d.ts

declare function leftPad(s: string, n: number): string;

converter/tsconfig.json

{
  "extends": "../tsconfig_base.json",
  "include": [
    "*.ts",
    "*.d.ts"
  ],
  "references": [
    { "path": "../utils" }
  ]
}

converter/length.ts

function formatInches(i: number) {
    if (i < 12) {
        return `${i}"`;
    }
    const ft = Math.floor(i / 12);
    i = i - ft * 12;
    return `${ft}' ${leftPad(i.toString(), 2)}`;
}

Expected behavior: Running tsc -b converter compiles successfully.

Actual behavior:

tsc -b converter --verbose
[13:10:29] Projects in this build:
    * utils/tsconfig.json
    * converter/tsconfig.json

[13:10:29] Project 'utils/tsconfig.json' is up to date because newest input 'utils/moreUtils.ts' is older than oldest output 'utils/moreUtils.js.map'

[13:10:29] Project 'converter/tsconfig.json' is out of date because output file 'converter/length.js' does not exist

[13:10:29] Building project '[...]/converter/tsconfig.json'...

converter/length.ts:18:22 - error TS2304: Cannot find name 'leftPad'.

18     return `${ft}' ${leftPad(i.toString(), 2)}`;
                        ~~~~~~~


Found 1 error.

(Local paths redacted)

As a side note, when compiling the packages into outFiles the compilation works just fine. (This can be seen in @RyanCavanaugh’s example repository from which mine is forked / when reverting to commit 0f66d20 of my example repository.)

Playground Link:

Example Repository: https://github.com/LucaVazz/ts-project-references-for-ambient

Related Issues: #22997 #25600 #26595 #26689 #26867

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, thanks for this fix. It would also be very helpful if there was a section in the project references documentation describing this use case.

I’ve been working on migrating a large number of TypeScript files from a monolithic MSBuild based system into a multi-project setup, and I can imagine that others in a similar position might run into this.

Perhaps adding a section right after https://www.typescriptlang.org/docs/handbook/project-references.html#msbuild would be relevant.

Perhaps something like:

Project Reference Usage without module imports

If your projects are not using module imports it is necessary to set the ‘module’ compiler option to ‘none’ in your composite projects so that projects that depend on them can find their emitted declarations at compile time:

"compilerOptions": {
    "module": "none", // Have to set module to none so that we can depend on this project as a non-module project
    "composite": true, // Mark this as a composite project so that other projects can depend on it
    "declaration": true, // A composite project must create d.ts files for other projects to reference
    "declarationMap": true, // Enable use of editor features like 'Go to Definition' and Rename to transparently navigate and edit code across project boundaries in supported editors.
    "outDir": "../built"
  }

@sheetalkamat tried out the 3.4RC and this is working brilliantly. Thanks

@LucaVazz I had chat with @RyanCavanaugh and it was discussed that we wont be including your input “.d.ts” file anytime in the referencing projects. So your repro code as is not going to work and its going to error even with the fix. The work around for that is to move those to “,ts” files instead and let the “.d.ts” be generated in output folder. The fix that I am going to do is to include output “*.d.ts” files from non module compilations without --out will be included in your referenced project as there is no other way to refer to those.

Adding @DanielRosenwasser Since we would need to discuss and decide what needs to happen here. It is yet to triaged and added to any milestone.