TS type check is checking my lib incorrectly when added it to an App project. #27125
-
Current BehaviorI’m have a NX monorepo with 1 app (my-app) and 1 lib (products). The products lib has the In the products lib I have this component being exported: export function Products() {
const myArr = [1, 2, 3];
const index: number = 0;
const value = myArr[index];
return (
<div>
{/* I don't need the optional operator because this project has noUncheckedIndexedAccess = false */}
<h1>{value.toString()}</h1>
</div>
);
} And I importing it to my-app: import { Products } from '@nx-test/products';
export function App() {
return (
<div>
<Products />
</div>
);
} When I run pnpm exec tsc --project ./apps/my-app/tsconfig.app.json --noemit
libs/products/src/lib/products.tsx:11:12 - error TS18048: 'value' is possibly 'undefined'.
11 <h1>{value.toString()}</h1>
~~~~~
Found 1 error in libs/products/src/lib/products.tsx:11
Yes, I have the Expected BehaviorType checking should be executed only for the files of my-app and not for its dependency libraries. GitHub Repohttps://github.com/joel-daros/nx-tsconfig-issue Steps to Reproduce
Nx ReportReport complete - copy this into the issue template
Node : 20.11.1
OS : darwin-x64
pnpm : 9.1.0
nx (global) : 18.1.2
nx : 18.3.4
@nx/js : 18.3.4
@nx/eslint : 18.3.4
@nx/workspace : 18.3.4
@nx/eslint-plugin : 18.3.4
@nx/react : 18.3.4
@nx/vite : 18.3.4
@nx/web : 18.3.4
typescript : 5.4.5
---------------------------------------
Registered Plugins:
@nx/vite/plugin
@nx/eslint/plugin Failure Logslibs/products/src/lib/products.tsx:11:12 - error TS18048: 'value' is possibly 'undefined'.
11 <h1>{value.toString()}</h1>
~~~~~
Found 1 error in libs/products/src/lib/products.tsx:11 Package Manager Versionpnpm 9.1.0 Operating System
Additional InformationNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Thanks for reporting this! This is the expected behavior for TypeScript with your setup. The
So, that's why TypeScript has a way to effectively split source code into multiple "projects", which is using project references and |
Beta Was this translation helpful? Give feedback.
Thanks for reporting this!
This is the expected behavior for TypeScript with your setup. The
my-app
project depends on the source code ofproducts
and when you runtsc -p apps/my-app/tsconfig.app.json
, the TS compiler creates ats.Program
for that configuration file. It doesn't use any other configuration file unless is extended or referenced. In this case, it extends thetsconfig.base.json
and it doesn't reference any other configuration file. Therefore, every file included in thets.Program
will be processed with theapps/my-app/tsconfig.app.json
configuration (plus the merged config from the extendedtsconfig.base.json
). The library source code is part of thets.Program
because:t…