Upon reviewing the responses here and persevering, I feel compelled to share my unique approach.
Working on a complex solution with over 100 subprojects, many of which involve rapidly changing NuGets, I was determined not to give up. My goal was to have my .NET object models, along with their interface/class representations in TypeScript, accessible through a single NuGet package to streamline dependency management. It's worth noting that I tested this method solely with my own object model, free from external dependencies, and exclusively in VS2022.
In this controlled scenario, everything functioned flawlessly without any hitches.
For the project containing the TS definitions:
Specify the build action for the necessary ts definitions to be included in the NuGet as "content." This will ensure they are packaged within the NuGet.
For the consumer side:
Adjust your package reference and add the following property/value:
<GeneratePathProperty>True</GeneratePathProperty>
This will create an MsBuild property referencing the local path where the restored NuGet file resides (crucial for builds across multiple machines like CI pipelines and build servers), eliminating the need for hardcoded absolute paths.
The generated property follows this format:
$(Pkg<PackageNameWithDotsBeingReplacedByUnderlines>)
Thus, a package named "MyPackage.Hello" would result in the variable $(PkgMyPackage_Hello)
.
Next, establish a new build target to copy files from the contentfiles folder of the restored package (now that it's restored, we can pinpoint the extracted path).
<Target Name="CopyImportedTypes" BeforeTargets="Build">
<ItemGroup>
<TsTypesToCopy Include="$(PkgMyPackage_Hello)\contentFiles\any\net6.0-windows10.0.20348\*.ts" />
</ItemGroup>
<Copy SourceFiles="@(TsTypesToCopy)" DestinationFolder="$(MSBuildProjectDirectory)\AnyProjectSubFolderIfDesired" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
</Target>
Be sure to customize the "Include" path according to your package (TFM, Platform, etc.). A convenient method to obtain the relative path is by navigating to the consuming project in Solution Explorer, expanding dependencies and packages, accessing the properties of the package containing your ts definitions, and examining the contentfiles.
This target runs before the actual build process (allowing immediate use of imported types) thanks to the BeforeTargets property. The ItemGroup defines items (such as source files) stored in @(TsTypesToCopy), utilized by the copy task.
Fortunately, VS typically assigns newly added files the correct build action automatically, so the fresh ts files should require no manual adjustments.