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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 3
- Comments: 15 (7 by maintainers)
Commits related to this issue
- Include the d.ts files from the referenced program without --out if its non module emit Fixes #29002 — committed to microsoft/TypeScript by sheetalkamat 5 years ago
- LPS-129801 Port "render.es.js" to TS Note this requires some horrible ugly casts for `Liferay`, because globals are nasty and all that. Figuring out how to declare that as an ambient type in some cen... — committed to brianchandotcom/liferay-portal by wincent 3 years ago
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:
@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.